Skip to content

Comment on Ask HN: Which line of code you have written will be executed the most?

Comments

I spent a week or so pouring through valgrind and profile output optimising the very heart of the code for a Monte Carlo simulation at a major investment bank. Some of the lines of code I wrote in that week will have been executed billions of times per day since I pushed them back in 2007 or so. They are still being used.

I got a good overall speedup (over 5% in code that was already pretty well-optimised) but by far the most valuable optimisation I did was one of the most basic - hoisting variables out of for loops. This kind of thing

    for(int i=0; i<something; ++i) {
       int x=0;
       //...do something with x  
    }

changes to...
    int x;
    for(int i=0; i<something; ++i) {
       x=0;
       //...do something with x  
    }

There are slightly more subtle versions of that, but at the time none of the compilers we were using (gcc 3.2, solaris 8 C++ compiler or Visual C++ 7) could optimise that without you doing the hoist yourself.
AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.