Path: utzoo!attcan!uunet!zaphod.mps.ohio-state.edu!swrinde!elroy.jpl.nasa.gov!decwrl!ucbvax!ulysses!ggs From: ggs@ulysses.att.com (Griff Smith) Newsgroups: comp.lang.c++ Subject: Re: why is this program slow? Summary: not doing the same thing Message-ID: <14167@ulysses.att.com> Date: 9 Jan 91 14:48:52 GMT References: <1991Jan9.002244.23398@news.cs.indiana.edu> Organization: AT&T Bell Laboratories, Murray Hill Lines: 37 In article <1991Jan9.002244.23398@news.cs.indiana.edu>, 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. The C program is written in terms of basic operations that the optimizer can easily improve; the C++ version isn't. For example: > a[0] = a[0] + b[0]; This is better written as a[0] += b[0]; though modern compilers can usually make the two cases equivalent. The C++ version is not so fortunate. You write > a = a + b; Since `a' is a structure, your `+' operator first has to store the sum in your `temp', then your `=' operator must copy it into `a'. This takes a string move at best, and a slow function call at worst. I tried redefining your `+' operator to be a `vector& +=' operator and my execution time on a tahoe became 6.0 seconds instead of 14.5. That's still not quite as good as 4.5 for the C version, but it explains most of the difference. > Could someone enlighten me? (yes, I looked at the C code generated > by CC, and am too dumb to understand it). C++ output isn't intelligible to most mortals; this mortal frequently has trouble with C++ input. I often find it easier to use adb to look at the assembly language. -- Griff Smith AT&T (Bell Laboratories), Murray Hill Phone: 1-201-582-7736 UUCP: {most AT&T sites}!ulysses!ggs Internet: ggs@ulysses.att.com