Hello,
What are the most highly regarded books on optimization / performance tuning of C/C++ code?
Hello,
What are the most highly regarded books on optimization / performance tuning of C/C++ code?
They are usually titled 'Algorithms', 'Data Structures' etc. Misleading, indeed ;-)
First, you need to have knowledge on how the language features are implemented on a common machine. Second, you need to study about OS and architecture specific optimization. It's impossible to make some code max optimized if you don't take account a specific compiler and machine.
One of the best books from "back in the day" was Zen of Code Optimization by Michael Abrash. I see used copies of it still available from Amazaon and other sellers.
While it's a bit dated in terms of CPU (386, 486, Pentiums) and compilers, it's so well written and informative that you can probably still learn a lot.
The following are the only things you need to do to get high performance out of computers:
If you're absolutely determined to do super-micro-optimised C/C++, then you should probably start by learning assembly language.
A classic book on the subject is Jon Bentley's Writing Efficient Code, which seems to be out of print. His 'Programming Pearls' and 'More Programming Pearls' also cover efficiency to some extent, though their scope is far wider than just efficiency.
Understanding and using the correct algorithms is another key factor in program efficiency.
One of the books you should check out is is Hacker's Delight by Henry S. Warren
It wouldn't do any harm to read Ulrich Drepper's 114-page article "What Every Programmer Should Know About Memory".
Agner Fog has some PDFs on optimization you can download for free.
I'd recommend "Effective C++: 55 Specific Ways to Improve Your Programs and Designs" by Scott Meyers.
It's not solely about "optimization" (whatever that is), but it's about writing code well. In general, writing code well tends to lead to better-performing code.
Well, It's not seemly to toot my own horn, but I tried to put a lot of that stuff in my book, which is now out of print. I've tried to say a lot of that stuff in my SO postings. The main points in a nutshell, which I know are useless without practice, are
Have an interest in information theory, both Shannon and algorithmic styles, and think about how it applies to software.
For performance tuning, there is a very simple and effective method, that some programmers know gets to the heart of the problem much faster than "measuring", and which, BTW, Jon Bentley concurs with.
If you do that, you come to see what in current practice makes software slow, and it is slavish over-design and galloping generality adhering to what are regarded as untempered "good design practices" leading to massive over-use of data structure.
If you look at the writing and maintaining of programs as a process in itself, with efficiency, you come to see the central role of domain specific languages and code generation. This correlates with, not against, run-time efficiency.
There is a methodology for analyzing any problem so that you can identify the range of solutions to it, each with pros and cons, and make intelligent choices with tradeoffs.
Hope that helps, Neil.
Code Complete
This isn't exactly a book on optimization; however, it contains many good general coding guidelines which could potentially lead to performance gains.
This is a category where old books should be approached with healthy skepticism. Compiler and chip technology both have come a long way in the last 10-15 years; you don't want to spend your time worrying about details that modern compilers will take care of, or worrying about numbers of processor instructions when it's I/O that matters now.
Here is a great blog of several microoptimizations especially when writing codecs or similar.
But beware! Most tricks can make your code almost unreadable (are you ready for IOCCC!), some optimisations may be known by the compiler itself so you do not have to do it manually.
As usual, use a profiler to find out hot spots and only try to optimize them (and only them).
There is also this book: The Art of Computer Systems Performance Analysis
You may want to check out Efficient C++: Performance Programming Techniques
The correct answer for "optimization", of course, is to design the program to have the needed performance properties: this will involve the general software architecture as well as choosing the right algorithms.
However, if you need micro-optimization to squeeze out the last few cycles of performance, Hacker's Delight, published by Addison-Wesley in 2003, is an excellent book.