Don’t assume the worst, trust the compiler

I am interested in security so when I saw a link on MSDN to a interview with Michael Howard on Port 25 I saw it as a nice opportunity to learn more about security in Vista.
 
Around 16:48 into it he mentions not to assume the worst and tells about an example on overflow issue and how the CPU knows it can happen so the code can check on it with the carry/overflow flags. He wrote an implementation in assembly, 560 byte. David LeBlanc co-authored a C++ template that weighed in at around 50k.
 
They ended up measuring the difference and the C++ template is faster!
 
When Michael asked some on the the reply back was that he was a moron. 🙂
 
What is the explanation? The C++ compiler has many years of work to make it compile into great optimized code, if is quite difficult to beat the experience and knowledge that are in the compiler and its optimizer.
 
Don’t assume the worst and start optimizing too early. When in doubt, stop and test, measure and compare different approaches.
 
Check out the interview.

Leave a Reply

Your email address will not be published. Required fields are marked *