Author: Kurt Guntheroth
Audience: Experienced C++ programmers
Reviewer: Mike James
If you think that the one rule about optimization is don't do it then you should read this book.
Optimization is something that you might think would be high on any programmers skill list, but it tends not to be due to a well known quote attributed to Donald Knuth:
premature optimization is the root of all evil.
As a result many books on optimization start off with the command "Don't!".
It is true that the most important aspect of code is how readable, understandable, and hence maintainable, it is. However this doesn't mean that optimization really is always evil. Sometimes it is essential.
This book is a really good introduction to the ideas that you need to approach optimization in the right way. It makes a clear case for when optimization is worth the effort and how to find out if it is in a particular case. In addition the book provides lots of anecdotes and personal experiences of the author. If you don't want to read to the end of this review and you are a C++ programmer just go and buy a copy.
The book opens with
"Hi, My name is Kurt,and I'm a code-aholic"
How could this book be a dud?
The first chapter gives you a taste of what is to come and justifies the use of optimization. Read the overview carefully; it will put you in the right frame of mind for the rest of the book.
Chapter 2 "Computer Behavior Affecting Optimization" contains much of the knowledge that just a short time ago you would not have needed to be made explicit. In days gone by, the majority of programmers using C and C++ came up via assembler and had a good grasp of hardware. Today C++ can be treated as a high-level, object-oriented language without any knowledge of how the machine does things. This chapter tells you things like - memory is slow, memory is not accessed in bytes and so on. You might know most of this, but it still doesn't hurt to be reminded.
Chapter 3 is a meditation on measuring performance - without which attempts at optimization are working blind. Chapter 4 is dedicated to C++ Strings which are a source of many unexpected problems if you are trying to optimize. Chapter 5 is a general look at the problem of optimizing algorithms and this cannot possibly be complete because there are too many algorithms to consider. It does a good job of covering the typical ways that algorithms are improved and what to look for in a good algorithm.
From the general we move on to consider C++ and dynamic variables. This is all about move semantics which is something that still baffles many a C++ programmer. The description of move semantics is good enough to make it worth the cost of the book. I won't say that you will necessarily understand move semantics at the end of the chapter but you will be closer to it.
From here the book settles down into some fairly standard optimization topics. Chapter 7 "Optimize Hot Statements" covers things you might have learned about in an ad-hoc way: cache loop end conditions, remove loop invariants, push loops into function to minimize call overheads and so on. The section on optimizing expressions might be an eye opener for many .
Chapter 8 is about using better libraries which is easy to say and more difficult to do. Chapter 9 is about searching and sorting - can there be anything left to say about this topic? The answer seems to be yes as this is very C++ oriented. The same is true for Chapter 10 on optimizing data structures and Chapter 11 optimizing I/O.
Chapter 12 is a good non-academic look at concurrency and the final one is on memory management.
As long as you are a reasonable C++ programmer then you should enjoy this book. It is written in a friendly style with lots of quotations with examples and anecdotal "war stories" from the author's career in programming, some going back to the days of the 8-inch floppy.
There is some value in the book if you are a C programmer and even if you program in some other language, but most of the advice targets C++. It is also a good idea to have some idea how C++ implements some of its facilities such as vtables and what virtual functions are all about.
If you want to create more efficient C++ programs then this book is a must and it's a good read if you just want to broaden your knowledge of C++.
To keep up with our coverage of books for programmers, follow @bookwatchiprog on Twitter or subscribe to I Programmer's Books RSS feed for each day's new addition to Book Watch and for new reviews.