---
title: Measuring performance
heading: How fast a program runs, and what makes it faster
eyebrow: C8 · Lesson 31 · Measuring performance
xp: 40
---

::: hook
"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.
:::

::: play
- 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.
:::
:::

```sim cpu
.data
vals: .word 3, 1, 4, 1, 5, 9, 2, 6
.text
    li s0, 0x20000       # the device registers; TIMER is at 0x20008
    lw s1, 8(s0)         # TIMER before the first version
    la t0, vals
    li t1, 0             # the index i
    li t2, 8             # n
    li a0, 0
loop1:
    bge t1, t2, done1
    slli t3, t1, 2       # 4i
    add t3, t0, t3       # the address of vals[i]
    lw t4, 0(t3)
    add a0, a0, t4
    addi t1, t1, 1
    j loop1
done1:
    lw s2, 8(s0)         # TIMER after
    sub a2, s2, s1       # instructions the first version took
    lw s1, 8(s0)         # TIMER before the second version
    la t0, vals
    addi t2, t0, 32      # the end pointer
    li a1, 0
loop2:
    beq t0, t2, done2
    lw t4, 0(t0)
    add a1, a1, t4
    addi t0, t0, 4
    j loop2
done2:
    lw s2, 8(s0)
    sub a3, s2, s1       # instructions the second version took
    ebreak
show: a0, a1, a2, a3
speed: 30
```

::: 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?

- [ ] 4 times
- [ ] 3.2 times
- [x] 2.5 times
- [ ] 1.8 times

::: reveal
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.
:::
:::

::: nameit
## The performance equation

$$t = N \cdot \text{CPI} \cdot T_c = \frac{N \cdot \text{CPI}}{f}$$

| Factor | Set by |
|---|---|
| N, instructions run | the algorithm, the compiler, the instruction set |
| CPI, cycles per instruction | the microarchitecture (pipelining, caches, stalls) |
| $T_c = 1/f$, 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 $F_i$ of the instructions take $\text{CPI}_i$ cycles each,

$$\text{CPI} = \sum_i F_i \cdot \text{CPI}_i$$

**MIPS** (million instructions per second) is $f/(\text{CPI} \cdot 10^6)$. 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 = \frac{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, $(S_1 S_2 \cdots S_n)^{1/n}$, which gives the same ranking whichever machine is the reference.
:::

::: 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?

::: solution
1. **CPI.**
   $$\begin{aligned} \text{CPI} &= 0.5(1) + 0.2(2) \\ &\quad + 0.1(1) + 0.2(1.5) \\ &= 1.3 \end{aligned}$$
2. **Time.**
   $$\begin{aligned} t &= \frac{10^7 \times 1.3}{5 \times 10^7}\ \text{s} \\ &= 0.26\ \text{s} \end{aligned}$$

**Answer:** the CPI is 1.3, and 10 million instructions take 0.26 s.
:::
:::

::: example
## An FFT takes 60% of a radio's processing time. A hardware accelerator runs it 10 times faster. What is the overall speedup?

::: solution
1. **Amdahl's law.**
   $$\begin{aligned} S &= \frac{1}{0.4 + 0.6/10} \\ &= \frac{1}{0.46} \\ &= 2.17 \end{aligned}$$
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.
:::
:::

::: 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?

::: solution
1. **Machine A.** Time per instruction of A's program:
   $$\frac{1.5}{2\ \text{GHz}} = 0.75\ \text{ns}$$
2. **Machine B.** It needs 0.8 times as many instructions:
   $$\frac{0.8 \times 2.5}{3\ \text{GHz}} = 0.667\ \text{ns}$$
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
kind: parts
given: Ca = 1 | 1.5; fa = 100 | 200; Cb = 2 | 2.5 | 3; fb = 350 | 450

## The same program runs on processor A (CPI {Ca}, {fa} MHz) and on processor B (CPI {Cb}, {fb} MHz).

::: part
answer: (Ca/fa)/(Cb/fb)
tolerance: 0.5%

### How many times faster is B than A? (A number below 1 means B is slower.)

::: misses
- [fb/fa] The clock isn't everything: compare CPI/f.
- [(Cb/fb)/(Ca/fa)] That's how much faster A is than B.
- [any] Not yet. Time is N × CPI/f; take A's over B's.
:::
:::

::: part
answer: fb/Cb
unit: MIPS
tolerance: 0.5%

### What is B's rate in MIPS?

::: misses
- [any] f/CPI.
:::
:::
:::

::: check
kind: parts
given: a = 0.4 | 0.5 | 0.6; l = 0.2 | 0.25; cl = 2 | 3 | 5; cb = 1 | 2; let b = 1 - a - l

## A program's instructions are {a} ALU at 1 cycle, {l} loads at {cl} cycles, and the rest branches at {cb} cycles.

::: part
answer: a + l*cl + b*cb
tolerance: 0.5%

### What is its average CPI?

::: misses
- [(1 + cl + cb)/3] Weight each kind by how often it occurs.
- [any] Not yet. $\sum F_i \cdot \text{CPI}_i$.
:::
:::

::: part
answer: l*cl/(a + l*cl + b*cb)*100
unit: %
tolerance: 0.5%

### What percentage of the cycles go to loads?

::: misses
- [l*100] That's the share of instructions. Loads take {cl} cycles each.
- [any] Not yet. The loads' cycles over all cycles.
:::
:::
:::

::: check
kind: parts
given: p = 0.5 | 0.7 | 0.9; s = 2 | 5 | 20

## A fraction {p} of a program's time is sped up by {s} times.

::: part
answer: 1/((1 - p) + p/s)
tolerance: 0.5%

### What is the overall speedup?

::: misses
- [s] Only part of the program got faster.
- [p*s] Amdahl's law: the new time is (1 − p) + p/s of the old.
- [any] Not yet. $1/((1 - p) + p/s)$.
:::
:::

::: part
answer: 1/(1 - p)
tolerance: 0.5%

### What is the largest speedup any improvement of that part could give?

::: misses
- [any] Let s go to infinity.
:::
:::
:::

::: check
given: p = 0.6 | 0.75 | 0.9; S = 1.5 | 2
answer: p/(1/S - (1 - p))
tolerance: 0.5%

## A fraction {p} of a program's time is in one routine. How many times faster must that routine get for the whole program to run {S} times faster?

::: right
Right. Solve $1/S = (1 - p) + p/s$ for s: {answer}.
:::

::: misses
- [S] The rest of the program doesn't speed up, so the routine has to make up for it.
- [any] Not yet. Solve Amdahl's law for s.
:::
:::

::: check
given: a = 1.2 | 1.5 | 0.8; b = 2.5 | 3; c = 0.9 | 1.1
answer: (a*b*c)^(1/3)
tolerance: 0.5%

## A new processor is {a}, {b} and {c} times as fast as the old one on three benchmarks. What is the geometric-mean speedup?

::: right
Right. The cube root of the product: {answer}.
:::

::: misses
- [(a + b + c)/3] That's the arithmetic mean, which a single outlier can dominate.
- [any] Not yet. The cube root of the product.
:::
:::

::: check
given: P = 10 | 50 | 200; f = 16 | 48 | 100; C = 1.3 | 2
answer: P/(f/C)
unit: nJ
tolerance: 0.5%

## A microcontroller draws {P} mW at {f} MHz and averages a CPI of {C}. How much energy does each instruction take?

::: right
Right. Power over instructions per second: {answer} nJ.
:::

::: misses
- [P/f] That's the energy per cycle. Each instruction takes {C} cycles.
- [any] Not yet. P/(f/CPI).
:::
:::

::: check
kind: choice

## 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?

- [ ] X is faster by a third
- [ ] X is faster, by an unknown amount
- [x] Nothing yet: Y might need far fewer instructions for the same work
- [ ] Y is faster, since a lower MIPS means more work per instruction

::: misses
- [1] The time also depends on how many instructions each needs for your program.
- [2] Y could still be faster, if its instructions each do more.
- [4] It might, or might not: you have to measure.
:::
:::

::: check
kind: parts
given: fa = 2 | 2.5; ca = 1.2 | 1.5; fb = 3 | 3.5; cb = 2 | 2.5; r = 0.8 | 1.1

## Machine A: {fa} GHz, CPI {ca}. Machine B: {fb} GHz, CPI {cb}, running {r} times as many instructions.

::: part
answer: ca/fa
unit: ns
tolerance: 0.5%

### How long does A take per instruction of its program?

::: misses
- [any] CPI ÷ frequency.
:::
:::

::: part
answer: (ca/fa)/(r*cb/fb)
tolerance: 0.5%

### What's B's speedup over A (below 1 if it's slower)?

::: misses
- [fb/fa] The clock is only one of three factors.
- [any] A's time ÷ B's time.
:::
:::
:::

::: check
kind: derivation
vars: p = 0..1, s = 1..1000, n = 1..1000

## A fraction p of a program's time is sped up by a factor s.

::: step
answer: 1/((1 - p) + p/s)

### What's the overall speedup?
:::

::: step
answer: 1/(1 - p)

### What's the most it can be, however large s gets?
:::

::: right
Right. Amdahl's law: the part you don't speed up sets the limit.
:::
:::

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

::: model
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

::: check
given: N = 1 | 8 | 50; C = 1.2 | 2; f = 20 | 100
answer: N*C/f*1000
unit: ms
tolerance: 0.5%

## {N} million instructions at CPI {C} on a {f} MHz clock: how long?

::: misses
- [any] N × CPI / f.
:::
:::

::: check
given: p = 0.3 | 0.8; s = 3 | 10
answer: 1/((1 - p) + p/s)
tolerance: 0.5%

## A fraction {p} is sped up {s} times. Overall speedup?

::: misses
- [any] Amdahl's law.
:::
:::

::: check
given: C = 1.1 | 1.6 | 2.4; f = 72 | 168
answer: f/C
unit: MIPS
tolerance: 0.5%

## What is the MIPS rating at CPI {C} and {f} MHz?

::: misses
- [any] f/CPI.
:::
:::
:::
