----- Forwarded message from firstname.lastname@example.org -----
Subject: Template code is 6.4 times slower than hand-written code
Content-Type: multipart/Mixed; boundary="openmail-part-041f486c-0000001d"
I have a particular application that makes heavy use of templates, and for
which g++ 2.95.1 is generating less than optimal code.
In fact, it appears that the overhead introduced by using the templates
results in a 6.4 slowdown in the innermost loop of the code. I think that
some of this slowdown might be attributed to my not using the right
optimization flags to the compiler, but I'm afraid that alot of it is because
g++ is not optimizing the instantiated code as aggressively as it could.
For instance, below are two versions of a loop that performs the following
double result = 0.0;
for (int ii=0; ii<x_nz; ii++)
result += x_storage[ii] * yy[x_indices[ii]];
This code is generated from "hand-written" code that is almost exactly as
that given above.
This code is generated from code that involved several levels of templates.
My understanding of the code is a little shaky, but I think I see at least
two problems with the later code.
First, some invariant values are repeatedly loaded from memory. In particular,
"movl -64(%ebp),%ecx" is, I think, reloading the value of x_indices. I have
using the __restrict keyword within the class definitions, but maybe I haven't
placed it correctly.
Second, there seem to be alot of "dead" writes. In particular,
This values are never read anywhere, as near as I can tell.
Could someone who knows a bit more about this take a look as what I'm doing
and tell me if I am doing something wrong?
 The complete source code for my "benchmark" question can be found at,
 The overhead from KCC 3.2f is 5.2. The overhead from the SGI MIPSpro
Compilers, version 7.2.1 is more than *42*!
 The code was generated using gcc 2.95.1 as follows,
% g++ -DNDEBUG -O6 -funroll-loops -malign-double -mcpu=pentiumpro \
-ansi -pedantic -W -Wall -Woverloaded-virtual -S -fno-unroll-loops \
Paul Stodghill <email@example.com>
Dept. of Computer Science, Upson Hall, Ithaca, NY 14853
Phone: 607-254-8838 FAX: 607-255-4428
----- End forwarded message -----
"Does `competition' have an abstract purpose?"
via, but not speaking for Deutsche Bank