views:

1588

answers:

19

Hello,

What are the most highly regarded books on optimization / performance tuning of C/C++ code?

+22  A: 

They are usually titled 'Algorithms', 'Data Structures' etc. Misleading, indeed ;-)

dirkgently
A: 

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.

GogaRieger
+8  A: 

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.

Van Gale
At the very least it is an amazing read.
peterchen
+2  A: 

The following are the only things you need to do to get high performance out of computers:

  • Care about performance
  • Properly understand how computers and programming languages work
  • Measure, measure, measure, measure

If you're absolutely determined to do super-micro-optimised C/C++, then you should probably start by learning assembly language.

Will Dean
+6  A: 

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.

Jonathan Leffler
+12  A: 

One of the books you should check out is is Hacker's Delight by Henry S. Warren

Igor Brejc
+1, amazing book if you like bit twiddling.
Bastien Léonard
+11  A: 

It wouldn't do any harm to read Ulrich Drepper's 114-page article "What Every Programmer Should Know About Memory".

timday
+12  A: 

Agner Fog has some PDFs on optimization you can download for free.

Manuel
+1, the best resource IMO.
Bastien Léonard
+12  A: 

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.

Bill Karwin
+5  A: 

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.

Mike Dunlavey
A: 

zen of assembly and pretty much anything else by Michael Abrash.

dwelch
A: 

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.

cakeforcerberus
A: 
none
+3  A: 

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.

Drew Hoskins
+1 - things change fast - even in the last 5 years micro-optimisation techniques that used to be worth considering are now either not applicable (and may even be counter-productive) or are being handled by a decent compiler. As with many disciplines, the general principles are more important, and survive much longer than the low level detailed stuff.
Paul R
A: 

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).

codymanix
+1  A: 
Vova
A: 

There is also this book: The Art of Computer Systems Performance Analysis

HomieG
+1  A: 

You may want to check out Efficient C++: Performance Programming Techniques

Nemanja Trifunovic
A: 

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.

Dale Hagglund
I just noticed that this book was already mentioned. Sorry for the duplication.
Dale Hagglund