<Backgound>
I am at the point where I really need to optimize C ++ code. I am writing a library for molecular modeling, and I need to add a new function. I already tried to add this function in the past, but then used virtual functions called nested loops. I had bad feelings about this, and the first implementation showed that it was a bad idea. However, this was good for testing the concept.
</ Background>
Now I need this function to be as fast as possible (well, without assembly code or GPU computation, it should still be C ++ and more readable than less). Now I am a little versed in class templates and class policies (from the excellent book by Alexandrescu), and I think that compiling compile-time code might be the solution.
However, I need to test the design before doing the tremendous work of introducing it into the library. The question is how best to test the effectiveness of this new feature.
Obviously, I need to enable optimization, because without this, g ++ (and possibly other compilers) will contain some unnecessary operations in the object code. I also need to very actively use the new function in the test, because a delta of 1-3 seconds can make the difference between a good and a bad design (this function will be called a million times in a real program).
The problem is that g ++ is sometimes “too smart” in optimization and can delete the whole loop, given that the result of the calculation is never used. I already saw this when I look at the output code of the assembly.
If I add some printing to stdout, then the compiler will be forced to do the calculations in a loop, but I will probably mostly compare the iostream implementation.
So, how can I do the right test of a small function extracted from the library? A related question: is it the right approach for conducting this kind of in vitro test on a small unit, or do I need the whole context?
Thanks for the tips!
It seems that there are several strategies, from compiler-specific options, that allow you to fine-tune more general solutions that should work with each compiler, for example volatile or extern .
I think I'll try it all. Thanks so much for all your answers!
c ++ optimization benchmarking
ascobol Jan 12 '09 at 14:46 2009-01-12 14:46
source share