What are the best books on how to optimize C++ code?
Depends on the scale/granularity you have in mind -- for large scale, where it matters most, my favorite is still Lakos' grand old book, dated tho it may be I don't think it's been surpassed.
Probably starting using an profiler to point you to the location that really needs speedup. I think a lot of people spend a lot of time in optimizing their code by guessing what eats up the time while normally the C++ optimizer of the compiler does a quiet well job.
So first identitify the bottleneck by use of a profiler. Then think about speeding up your code.
Michael Abrash has written some books on optimization. His focus will be on using x86 assembly code, but the underlying concepts/reasons/techniques for optimization will apply to many programming languages, especially C++. And when not using assembly code, he will use C or C++ so it might be relevent.
For anyone trying to optimize their code, his books will be more of an inspiration than actual reference.
I really liked Bulka & Mayhew's "Efficient C++: Performance Programming Techniques".
This isn't exactly specific to C++ but personally I think the best optimizations are done on an algorithm/data structure level and thus I'd strongly recommend Programming Pearls: http://www.cs.bell-labs.com/cm/cs/pearls/
My favorite low-level source is actually a website: http://www.agner.org/optimize/
Michael Abrash's Graphics Programming black book can be downloaded, legitimately, from here ... for free!. A brilliant in depth description of assembler optimisation even if it is pretty graphics-centric
Code Optimization: Effective Memory Usage, Kris Kaspersky
Write Great Code Vol. I/II, Randall Hyde
You can find books on algorithms and compiler (low-level) optimization. Those are fine, as far as they go, but large real software has a completely different problem.
Large real software is dominated, in my experience, by slowness due to "galloping generality", and it requires a different approach to optimization, as in this example. I have yet to see a book about this.*
*Except for my own (modest cough), which is long out of print.
Some books that I've found very useful, and although they are not specific to the issues of optimization, they do touch on a lot of the issues, are Scott Meyers books: Effective C++, More Effective C++ and Effective STL.
I'm going to give you a generalized answer:
- Make sure the program is working correctly.
- Consider changing your algorithms with faster ones.
- Consider the impact of your data structures. Memory locality, CPU cache hits and the like will give you performance gains.
- If you can't find candidates for the two above, can you change the original problem? E.g., instead of solving something with an exact solution, is an approximation good enough?
- Improve your code; make it clearer, remove redundant stuff.
- Perform micro-optimizations
And remember to always time and profile your code.
Some very good books for improving your code are the Effective C++ series by Scott Meyers. Couple those with the ones other people are suggesting, and you should be well on your way.
As for micro-optimizations, some common techniques are increasing CPU cache hits, improving branch prediction, unrolling loops, consider CPU vector operations and so on.
These are general techniques, but to get the last drop of performance you will have to tie everything closely to the CPU architecture you're working on.
Ulrich Drepper's 114-page article "What Every Programmer Should Know About Memory" is essential reading, I think. It won't tell you much about how to speed up code using algorithmic optimizations or concurrency, but it will definitely help you get the most out of the memory and cache systems your code is running on.
Really knowing your algorithms is a good way to optimize you code. Two kinds of book help me do this. Algorithmic books like
Donald E. Knuth, The Art of Computer Programming
and then the puzzle books like
Jon Bentley, Programming Pearls
I know that they aren't specific for C++ but a lot of optimizations can probably be applied independently of language.