Profiling the Code
Now that we know what performance is and how to measure it, we can start speeding up our programs. This is, however, not an easy process. For starters, we must know which parts of the program are slowing us down, and that can sometimes be hard to determine.
We can, of course, start by guessing. This will sometimes work, but it will often just result in wasted time and frustration. It turns out that it is usually hard to tell what will be a slow part of a complex program. Even if we start by guessing, it is good if we can then confirm our assumption before we start rewriting the code.
We can do that by adding measurement code, which works great if a program is small or if we know which specific part we want to improve, but most of the time, it is better to use a specialized tool: a profiler.
In this chapter, we’ll look into both approaches, manual profiling and using an automated tool.
We will cover the following topics:
- Why is it better to measure than to guess?
- How to manually determine which part of the program is the slowest
- What tools can we use to find the slow parts of a program?