ssimmons@convex.com (Steve Simmons) (01/09/91)
Use the +w switch on the compiler.... You will see that many of your inline routines were converted to outline routines, actual routine calls were made. Inline is only a suggestion for optimization, not an imperative order. The compiler may decide to make a separate routine for the inline routine and jump to that. Thus, it becomes more expensive to use inline routines because the actual code for the routine is copied for each compilation unit that calls it at least once. Therefore, you have both the call cost and the cost of replicating the code in "n" places. As implemented, inlining in C++ can be more painful than useful. It is best to use it only on simple retreival or storage on one data member. Secondly, you are passing back a copy of the vector in the operator+. You would be better be passing back a reftype. The reftype is really a pointer. Maybe something like this... vector& operator+(vector& u, vector& v) { static 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; } Thank you. Steve Simmons #include <math.h> #include <stream.h> class vector { protected: double data[3]; public: vector(); vector(double, double, double); void set(double, double, double); void operator=(vector&); friend ostream& operator<<(ostream&, vector&); friend vector operator+(vector&, vector&); }; inline vector::vector() { } inline vector::vector(double a, double b, double c) { data[0] = a; data[1] = b; data[2] = c; } inline void vector::set(double a, double b, double c) { data[0] = a; data[1] = b; data[2] = c; } inline void vector::operator=(vector& v) { data[0] = v.data[0]; data[1] = v.data[1]; data[2] = v.data[2]; } 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; } inline ostream& operator<<(ostream& s, vector& t) { return s << "(" << t.data[0] << ", "<< t.data[1] <<", " << t.data[2] <<")"; } main() { vector a(0.0, 0.0, 0.0); vector b(1.0, 2.0, 4.0); for (int i = 0; i < 1000000; i++) a = a + b; cerr << a << "\n"; } ------------------------------------------------------------------------------- stderr with +w CC +w foo.C: "foo.C", line 59: warning: operator <<() too large for inlining "foo.C", line 68: warning: initializer for non-const reference not an lvalue (anachronism) "foo.C", line 68: warning: temporary used for non-const vector & argument "foo.C", line 70: warning: operator <<() too complex for inlining "foo.C", line 70: warning: out-of-line copy of operator <<() created "foo.C", line 71: warning: no value returned from main() cc foo.c -lC