C8 · Lesson 31 · Measuring performance
How fast a program runs, and what makes it faster
"This processor runs at 200 MHz and that one at 100 MHz" says less than it seems to. How long a program takes depends on three things multiplied together: how many instructions it runs, how many clock cycles each takes on average, and how long a cycle is. A faster clock can lose to a better instruction set or a smarter compiler, and speeding up one part of a program can only help as much as that part matters. Measuring well, and knowing which of the three factors a change affects, is what lets an engineer pick a processor or decide where an optimization is worth the effort.
- The program adds up eight numbers twice: first by index, recomputing each element's address, then by walking a pointer. It reads the board's TIMER (which counts instructions) before and after each version.
- Run it. Both versions find 31, but a2 = 63 instructions for the indexed loop and a3 = 46 for the pointer loop: 7 instructions a pass against 5.
- On a processor that takes the same cycles for each instruction, the second version is 63/46 = 1.37 times as fast, with nothing changed but the instruction count.
Predict
80% of a program's time is spent in one routine, which is then made 4 times faster. How much faster is the whole program?
2.5 times. The routine's 80% shrinks to 20%, but the other 20% is untouched: 0.2 + 0.2 = 0.4 of the old time. Even an infinitely fast routine would leave that 20%, a limit of 5 times.
Name it
The performance equation
| Factor | Set by |
|---|---|
| N, instructions run | the algorithm, the compiler, the instruction set |
| CPI, cycles per instruction | the microarchitecture (pipelining, caches, stalls) |
| , the clock period | the logic's critical path and the transistors |
Changing one often moves another: a deeper pipeline raises f but adds stalls to CPI.
Average CPI from a mix. If a fraction of the instructions take cycles each,
MIPS (million instructions per second) is . It's a poor way to compare different instruction sets, because one may need more instructions for the same job.
Speedup and Amdahl's law
Speedup is old time over new time. If a fraction p of the time is sped up by a factor s,
which can never exceed , however large s gets. Work on the part that takes the most time first.
Benchmarks
A benchmark is a program run to compare machines. Results over several benchmarks are summarized by the geometric mean of the speedups, , which gives the same ranking whichever machine is the reference.
Worked example
A program's instructions are 50% ALU (1 cycle), 20% loads (2 cycles), 10% stores (1 cycle) and 20% branches (1.5 cycles on average). What is its CPI, and how long do 10 million instructions take at 50 MHz?
Show the solution
- CPI.
- Time.
Answer the CPI is 1.3, and 10 million instructions take 0.26 s.
Worked example
An FFT takes 60% of a radio's processing time. A hardware accelerator runs it 10 times faster. What is the overall speedup?
Show the solution
- Amdahl's law.
- The limit. The remaining 40% now dominates; even an infinitely fast FFT would give only .
Answer an overall speedup of 2.17.
Worked example
Machine A runs at 2 GHz with a CPI of 1.5. Machine B runs at 3 GHz with a CPI of 2.5, and its compiler needs only 0.8 times as many instructions. Which is faster?
Show the solution
- Machine A. Time per instruction of A's program:
- Machine B. It needs 0.8 times as many instructions:
- Compare. B is 1.125 times faster, although its CPI is worse and the clock alone would have suggested 1.5 times.
- The lesson. Only the product of all three factors predicts the time, which is why benchmarks run real programs.
Answer B is faster, by a factor of 1.125.
Check
The same program runs on processor A (CPI 1, 100 MHz) and on processor B (CPI 3, 450 MHz).
(a)
How many times faster is B than A? (A number below 1 means B is slower.)
(b)
What is B's rate in MIPS?
Check
A program's instructions are 0.5 ALU at 1 cycle, 0.2 loads at 5 cycles, and the rest branches at 2 cycles.
(a)
What is its average CPI?
(b)
What percentage of the cycles go to loads?
Check
A fraction 0.7 of a program's time is sped up by 2 times.
(a)
What is the overall speedup?
(b)
What is the largest speedup any improvement of that part could give?
Check
A fraction 0.75 of a program's time is in one routine. How many times faster must that routine get for the whole program to run 1.5 times faster?
Check
A new processor is 1.5, 2.5 and 1.1 times as fast as the old one on three benchmarks. What is the geometric-mean speedup?
Check
A microcontroller draws 50 mW at 16 MHz and averages a CPI of 2. How much energy does each instruction take?
Write units the way you normally would: 1.5 nJ, 1500 pJ and 0.0015 µJ are the same answer.
Check
Processor X runs 400 MIPS and processor Y runs 300 MIPS, with different instruction sets. What can you conclude about how fast they run your program?
Check
Machine A: 2 GHz, CPI 1.2. Machine B: 3.5 GHz, CPI 2.5, running 0.8 times as many instructions.
(a)
How long does A take per instruction of its program?
Write units the way you normally would: 1.5 ns, 1500 ps and 0.0015 µs are the same answer.
(b)
What's B's speedup over A (below 1 if it's slower)?
Check
A fraction p of a program's time is sped up by a factor s.
Step 1
What's the overall speedup?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
What's the most it can be, however large s gets?
Explain why
Why does making one routine much faster often give a disappointing overall speedup?
The program's time is the sum of the time in that routine and the time everywhere else, and the improvement only shrinks the first part. Once the routine is fast, the untouched remainder dominates: with 70% of the time in the routine, even an infinitely fast version leaves 30%, a speedup of only 3.3. That's Amdahl's law. It says to profile first and spend the effort on whatever takes the largest share of the time, and to expect each further gain to be smaller.
Practice
Keep practicing
10 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.