Title: Returning objects from function calls efficiently PROBLEM: Peter Shirley (shirley@iuvax.cs.indiana.edu) I have a C++ program that takes about 170% as long as a similar C program. I've run it on a vax and a sun with and without big-O. I've inlined everything I can, and I don't understand the slowdown. inline vector operator+(vector& u, vector& v) { vector temp; temp.data[0] = u.data[0] + v.data[0]; temp.data[1] = u.data[1] + v.data[1]; temp.data[2] = u.data[2] + v.data[2]; return temp; } RESPONSE: Steve Clamage, TauMetric Corp (steve@taumet.com) This is your problem. You create a local object, then return it by value. This must then be copied into the target variable. [...stuff deleted...] Try rewriting operator+ as: inline vector operator+(vector& u, vector& v) { return vector(u.data[0] + v.data[0], u.data[1] + v.data[1], u.data[2] + v.data[2]); } Many C++ compilers are smart enough not to create a temporary object, but to construct the new vector directly in the target variable. RESPONSE: Jamshid Afshar The optimization Steve is talking about is allowed for either version of the code. The "return value optimization" is used when you write: Vector c (a+b); An UNoptimizing compiler would copy-construct `c' using the temporary returned by `a+b' and then destroy the temporary. An optimizing compiler would not need to call the copy-constructor or destroy any temporary. Instead of constructing `temp' (or an unnamed temporary) as a local variable, `operator+()' would be passed a hidden parameter which is a pointer to the location where the return object should be placed. The above initialization of `c' would get translated into something like: char __c_buf[sizeof(Vector)]; __operator_plus( a, b, __c_buf ); // pass in raw memory Vector* c = (Vector*)__c_buf; // it becomes a Vector //... use `c' See ARM 12.1c for a complete explanation of this optimization. Some compilers which optimize Steve's version might not be smart enough to optimize the original version, but I know that our version of cfront optimizes both versions equally.