I know that you should only optimize things when it is deemed necessary. But, if it is deemed necessary, what are your favorite low level (as opposed to algorithmic level) optimization tricks.
For example: loop unrolling.
I know that you should only optimize things when it is deemed necessary. But, if it is deemed necessary, what are your favorite low level (as opposed to algorithmic level) optimization tricks.
For example: loop unrolling.
In SQL, if you only need to know whether any data exists or not, don't bother with
COUNT(*)
:If your
WHERE
clause is likely return multiple rows, add aLIMIT 1
too.(Remember that databases can't see what your code's doing with their results, so they can't optimise these things away on their own!)
One of the most useful in scientific code is to replace
pow(x,4)
withx*x*x*x
. Pow is almost always more expensive than multiplication. This is followed byto
But my favorite low level optimization is to figure out which calculations can be removed from a loop. Its always faster to do the calculation once rather than N times. Depending on your compiler, some of these may be automatically done for you.
Counting down a loop. It's cheaper to compare against 0 than N:
Shifting and masking by powers of two is cheaper than division and remainder, / and %
Edit
because SIZE is 32 or 2^5.
Compilers do a lot better job of it than you can.
vfork()
instead offork()
beforeexec()
I've found that changing from a pointer to indexed access may make a difference; the compiler has different instruction forms and register usages to choose from. Vice versa, too. This is extremely low-level and compiler dependent, though, and only good when you need that last few percent.
E.g.
vs.