Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!radar!cadillac!Pkg.Mcc.COM!steve From: steve@Pkg.Mcc.COM (Steve Madere) Newsgroups: comp.lang.c++ Subject: Re: why is this program slow? Message-ID: <1991Jan9.101212@Pkg.Mcc.COM> Date: 9 Jan 91 16:12:12 GMT References: <1991Jan9.002244.23398@news.cs.indiana.edu> Sender: news@cadillac.CAD.MCC.COM Reply-To: steve@Pkg.Mcc.COM (Steve Madere) Lines: 94 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. | Could someone enlighten me? (yes, I looked at the C code generated | by CC, and am too dumb to understand it). | | for ( i = 0; i < 1000000; i++) | { | a[0] = a[0] + b[0]; | a[1] = a[1] + b[1]; | a[2] = a[2] + b[2]; | } | 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; | } | for (int i = 0; i < 1000000; i++) | a = a + b; | | cerr << a << "\n"; | } At this point the cause of the speed problems should be obvious. Your C++ program is not quite equivalent to your C program. To make them equivalent, you should have the following C program segment substituted for the one above. for (i=0;i<1000000;i++) { temp = calloc(3,sizeof(*temp)); temp[0] = a[0] + b[0]; temp[1] = a[1] + b[1]; temp[2] = a[2] + b[2]; a[0]=temp[0]; a[1]=temp[1]; a[2]=temp[2]; free(temp); } Once this is done, I think your two programs will probably run at the same speed :-(. This is a problem with C++ that I have known about for some time. The lure of simplified notation leads one to try to implement vector and matrix operations with special classes that will hide the complexity. Unfortunately, all of those temporary variables that must be created to realize any mathematical operations really waste CPU time. It takes a finite amount of time to allocate space, copy data, and free up the space again. In many cases it is a significant percentage of the total run time of the program. A friend of mine at UCSD has worked on this problem for some time and concluded that what one needs is some kind of matrix awareness in the compiler to recognize when operations can be somehow combined. Clearly this is not the kind of thing that C++ was designed to do. It is appropriate to add matrix awareness to a computational language like FORTRAN but C++ is not a scientific computation language. (no flames please, I just mean that its PRIMARY purpose is not scientific computation). Maybe what one needs is a preprocessed language that takes matrix operations, turns them into optimized C code which you can link with your C++ program. Any suggestions from the peanut gallery? Steve Madere steve@pkg.mcc.com