home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!microsoft!wingnut!pauljo
- From: pauljo@microsoft.com (Paul Johns)
- Subject: Re: Do temps hurt performance of overloaded +
- Message-ID: <1992Dec17.210748.24077@microsoft.com>
- Date: 17 Dec 92 21:07:48 GMT
- Organization: Microsoft, Redmond WA USA
- References: <1gofq7INN8q@golden.kaleida.com>
- Lines: 64
-
- Whether or not temporaries are created is up the implementation,
- provided that the implementation meets the other requirements
- of the language.
-
- In your case, you've done about all you can to avoid creation
- of temporaries:
-
- You've made the parameters to Complex::operator +
- references, which will keep copies of the operands
- from being made for pass-by-value.
-
- You've made the function inline, which allows the
- compiler to optimize away the temporary for the
- return value if it can.
-
- However, for the statement "c = a + b;" the compiler would have to
- construct a temporary for the return value, then use the assignment
- operator to assign it. I don't think that the compiler is allowed
- to optimize this away. (Although since you don't define
- Complex::operator =, some compilers could optimize if you hadn't
- provided a copy constructor. Usually if you provide a copy constructor,
- you'll need an assignment operator; in your case, the copy constructor
- wasn't really necessary.)
-
- If you'd waited to declare c, then written "Complex c = a + b;",
- the compiler could eliminate the temporary if it wanted to.
- The elimination of temporaries (and their constructor/destructor
- calls) is the main reason for allowing declarations to appear
- anywhere in C++.
-
- As it is now, you'll have constructor calls for c and the temporary,
- a call to c's assignment operator, and destructor calls for c and
- the temporary. Changing the assignment to a declaration would
- at minimum change the assignment operator call to a copy constructor
- call; but it would probably eliminate the assignment operator call
- along with the construction and destruction of the temporary.
-
- So, your code would be more efficient if you waited to declare c
- until you could initialize it. This is SOP for C++ anyway, and
- good programming practice.
-
- I'm not sure how smart most compilers will be about passing
- "a + b" to operator new. You may have no choice but to use a
- temporary here.
-
- Note, however, that in all cases the creation of temporaries
- is pretty much comparable to what you'd get for built-in types.
-
- There's one other change I'd make: pass your parameters in by
- const reference rather than plain reference. Starting in version
- 2.1, you cannot preform any conversions on parameters that are
- passed by plain reference. That would mean that you could ONLY
- pass two Complexes to the operator function--you could not pass
- one int or double or anything else. Also, you could not pass
- a const Complex as it is now. So the prototype would become:
-
- Complex::operator +(const Complex &x, const Complex &y);
-
- (On the other hand, using a non-const reference means that your
- users will have to make type conversions explicit, which might not
- be a bad idea when you're seeking efficiency through not creating
- temporaries.)
-
- // Paul Johns
-