Path: utzoo!censor!geac!torsqnt!hybrid!scifi!bywater!uunet!taumet!steve From: steve@taumet.com (Stephen Clamage) Newsgroups: comp.lang.c++ Subject: Re: why is this program slow? Message-ID: <547@taumet.com> Date: 9 Jan 91 16:38:34 GMT References: <1991Jan9.002244.23398@news.cs.indiana.edu> Organization: Taumetric Corporation, San Diego Lines: 57 shirley@iuvax.cs.indiana.edu (peter shirley) writes: >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; >} This is your problem. You create a local object, then return it by value. This must then be copied into the target variable. The equivalent C code looks like this: typedef struct { double data[3]; } vector; vector plus( vector* this, vector* u, vector* v) { ...the body is the same as above... } main() { vector u, v, w; ... w = plus(&w, &u, &v); ... } Compare w = plus(&w, &u, &v); to w.data[0] = u.data[0] + v.data[0]; w.data[1] = u.data[1] + v.data[1]; w.data[2] = u.data[2] + v.data[2]; There is an extra structure copy using plus() instead of direct code, and the copy is in the inner loop. 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. -- Steve Clamage, TauMetric Corp, steve@taumet.com