C831 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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?

Name it

The performance equation

t=N⋅CPI⋅Tc=N⋅CPIf
FactorSet by
N, instructions runthe algorithm, the compiler, the instruction set
CPI, cycles per instructionthe microarchitecture (pipelining, caches, stalls)
Tc=1/f, the clock periodthe 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 Fi of the instructions take CPIi cycles each,

CPI=∑iFi⋅CPIi

MIPS (million instructions per second) is f/(CPI⋅106). 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,

S=1(1−p)+p/s

which can never exceed 1/(1−p), 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, (S1S2⋯Sn)1/n, 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
  1. CPI. CPI=0.5(1)+0.2(2)+0.1(1)+0.2(1.5)=1.3
  2. Time. t=107×1.35×107s=0.26s

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
  1. Amdahl's law. S=10.4+0.6/10=10.46=2.17
  2. The limit. The remaining 40% now dominates; even an infinitely fast FFT would give only 1/0.4=2.5.

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
  1. Machine A. Time per instruction of A's program: 1.52GHz=0.75ns
  2. Machine B. It needs 0.8 times as many instructions: 0.8×2.53GHz=0.667ns
  3. Compare. B is 1.125 times faster, although its CPI is worse and the clock alone would have suggested 1.5 times.
  4. 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°.

Explain why

Why does making one routine much faster often give a disappointing overall speedup?

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.