CPU Time Equation
In one sentence: The CPU time equation says how many seconds a processor spends on a program, using three counts: the steps the program takes, the clock ticks each step needs, and how fast the clock ticks.
Read it out loud: “T sub C P U equals I C times C P I times t sub clock, which is the same as I C times C P I, divided by f. T sub C P U is the CPU time, the number of seconds the processor spends on the program. I C is the instruction count, the number of steps the program takes. C P I is the cycles per instruction, the average number of clock ticks each step needs. t sub clock is the length of one clock tick in seconds, and f is the clock frequency in hertz, which is ticks per second.”
Before you start
The ideas and signs this page uses, explained from zero. For powers of ten, Greek letters and other notation in general, see Math basics.
- Processor, instruction and instruction count (IC)
A processor is the chip that carries out a program. A program is made of instructions, tiny steps such as adding two numbers, comparing two values or copying a value from one place to another. The instruction count, written , is how many of these steps the program runs in total. It is a plain count, so it has no unit. A program with 4,000,000,000 instructions has .
- Clock, clock cycle and hertz (Hz)
A processor has a clock, a steady beat like a metronome. Each beat is a clock cycle, the smallest tick of time the processor works in. The frequency, written , counts beats per second and is measured in hertz (Hz), where 1 Hz means one cycle per second. A 2 GHz processor ticks 2 billion times every second.
- Powers of ten, and the prefixes giga (G) and nano (n)
A power of ten like means 10 multiplied by itself nine times, which is 1,000,000,000. A scientific notation number such as is 4 multiplied by that. The prefix giga (G) means , so 2 GHz is Hz. The prefix nano (n) means , a billionth, so 0.5 ns is s. A negative exponent means divide: .
- CPI, cycles per instruction
CPI stands for cycles per instruction. Different instructions take different numbers of clock cycles, so CPI is an average: the total number of cycles divided by the number of instructions. A CPI of 1.2 means the program needs 1.2 cycles per instruction on average, with some instructions taking one cycle and others taking more. It has no unit.
- Units that cancel, like cycles divided by cycles per second
Units can be multiplied and divided like numbers, and the same unit on the top and bottom cancels. Multiplying instructions by cycles per instruction leaves cycles, because the instructions cancel. Dividing cycles by cycles per second leaves seconds, because the cycles cancel. This check of units is a quick way to catch a wrong formula.
- Subscripts, like and
A small label written below a letter is a subscript, which names the quantity. In the letter stands for time, and the subscript says it is the CPU time. In the letter stands for time, and the subscript says it is the length of one clock tick. A subscript is a name, not a multiplication, so is one quantity.
What each symbol means
| Symbol | Meaning |
|---|---|
| The CPU time: the number of seconds the processor spends running this program. It is measured in seconds (s). A short program may take a fraction of a second, and the example below gives 2.4 s. | |
| The instruction count: how many instructions the program runs in total. It is a plain count, so it has no unit. The example uses 4 billion instructions, which is instructions. | |
| Cycles per instruction: the average number of clock cycles one instruction takes in this program. It is an average, so it has no unit. The example uses 1.2 cycles per instruction, which means some instructions take one cycle and others take longer. | |
| The clock period: the length of one clock cycle, measured in seconds. It is the time of one tick of the clock, and it equals . A 2 GHz clock has a period of s, which is 0.5 nanoseconds. | |
| The clock frequency: how many clock cycles happen each second, measured in hertz (Hz). One hertz is one cycle per second. The example uses GHz, which is Hz, or 2 billion ticks per second. |
The formula piece by piece
Start with the number of instructions in the program. This is how many steps the processor must take. A longer program has a bigger , and the time it takes grows in step with it. In the example .
The average number of clock cycles that one instruction needs. A program whose instructions each take 2 cycles has . It is an average over the whole program, so it depends on the mix of instructions, not on the clock speed.
The total number of clock cycles the program needs. Each instruction takes CPI cycles on average, so the instructions multiplied by the cycles each one takes give the cycles in total. In the example it is cycles.
The length of one clock cycle, which is one tick. It is found from the frequency as . For a 2 GHz clock, one tick lasts seconds, half a billionth of a second.
The whole formula as one sentence: the CPU time equals the number of instructions, times the cycles each one takes on average, times the length of one clock cycle. Written the second way, it is the total cycles divided by the clock frequency, and both forms give seconds.
What the formula is doing, and why
The CPU time equation splits the running time of a program into three factors. The first is how many instructions the program runs, . The second is how many clock cycles each instruction takes on average, . The third is how long one clock cycle lasts, . Multiplying the three gives the time in seconds. The equation is often called the iron law of processor performance.
Why multiply the first two? Each instruction takes cycles on average, so the whole program needs cycles. A program with 1,000 instructions that each take 2 cycles needs cycles. The units agree with this: instructions times cycles per instruction leaves cycles, because the instructions cancel.
Why divide by the frequency? One clock cycle lasts seconds. A faster clock has a bigger , so each cycle is shorter. Multiplying the cycle count by the cycle length gives seconds. Since , the same answer is written as . Cycles divided by cycles per second leaves seconds, which is the unit we want, so a bigger gives a smaller time.
The factors can trade off. A compiler, the program that turns code into instructions, can change by producing fewer instructions. The design of the processor and the mix of instructions change . The clock frequency sets how fast the cycles come. These levers can cancel. If doubles while the clock is also made twice as fast, the time stays the same, because the doubled cycle count is divided by a doubled frequency.
When does it apply? The equation is an average. The must reflect the real mix of instructions and any waiting, such as waiting for data from memory, so it should be measured on the real program. A faster clock alone does not guarantee a faster program, because the number of cycles still matters.
An everyday picture
Think of printing a stack of 200 pages on a press. Each page passes through the press a few times, once for each color, and the press runs at a fixed speed of passes per second. The total print time is the number of pages times the passes per page, divided by the press speed. The CPU time equation works the same way. The instructions are the pages, the cycles per instruction are the passes, and the clock frequency is the press speed.
Worked example: A photo-filter program on a 2 GHz laptop
A photo-filter program runs instructions, which is 4 billion. On average each instruction takes clock cycles. The laptop’s processor has a clock of GHz, which is cycles per second. The numbers are made up for this example, to show how the formula works. They are not measurements of a real program.
- Write down the three inputs. We need instructions, cycles per instruction, and Hz. The power of ten is shorthand, so means 4 with nine zeros, or 4,000,000,000. Writing the numbers this way keeps the multiplication easy to follow.
- Find the total number of cycles. Multiply the instructions by the cycles per instruction: . First multiply the ordinary numbers, . The power of ten stays as , so the total is cycles. That is 4.8 billion ticks of the clock.
- Find the length of one clock cycle. The period is s. Dividing 1 by 2 gives 0.5. Dividing by is the same as multiplying by , so s. That is 0.5 ns, half a billionth of a second.
- Multiply the cycles by the length of one cycle. s. Multiply 4.8 by 0.5 to get 2.4. For the powers of ten, adds the exponents to give , which is 1, so they cancel. The cycles cancel too, and the answer is s.
- Check with the other form. Dividing the cycles by the frequency should give the same answer: s. Here the powers of ten divide, , so the result is . The units are cycles divided by cycles per second, which leaves seconds.
- Try a faster clock. At 4 GHz the frequency is Hz, so s. The program needs the same number of cycles, but each second now holds twice as many of them, so the time is halved.
- Give the answer a plain size. 2.4 seconds is a little more than two seconds, which a person waiting on the screen would notice. The same program on the 4 GHz processor finishes in about one and a fifth seconds. Both answers assume the instruction count and the CPI stay the same.
Answer: The program takes 2.4 seconds on the 2 GHz processor. That is 4.8 billion clock cycles, each lasting 0.5 ns. Doubling the clock to 4 GHz halves the time to 1.2 seconds, if the instruction count and the CPI stay the same.
The numbers in this example are recomputed by script every time the site is built.
Common mistakes
- Multiplying by the clock frequency instead of dividing by it. Time is cycles divided by cycles per second, so a faster clock, with a bigger , gives a smaller time.
- Mixing up and . is a count of instructions, and is an average number of cycles per instruction. Their product is a count of cycles, which is what the clock counts.
- Using both the clock period and the frequency in the same calculation. Use either or , because , so using both counts the clock twice.
- Taking from a different program or from a spec sheet. depends on the code that runs, so it should be measured on the real workload.
Where you will meet it
- Comparing processors in benchmarks and reviews, where clock speed and CPI both differ between chips.
- Compiler optimization, which tries to cut the instruction count or the cycles per instruction of the code it produces.
- Processor design, where features such as pipelining, which overlaps the steps of many instructions, and caches are judged by how much they lower CPI.
- Explaining why a higher clock rate does not always give a faster program.
Questions people ask
What is CPI?
Cycles per instruction: the average number of clock cycles one instruction takes in a given program. Modern processors can finish more than one instruction in a single cycle, so their CPI can be below 1. The inverse, , is the number of instructions per cycle, called IPC.
Is a higher clock rate always better?
Not on its own. The clock rate matters together with and . A processor with a higher clock can still be slower overall if its is much higher, because the product is the number of cycles the processor must run.
Where does the instruction count come from?
It depends on the algorithm, the compiler and the input data. Profilers, which are tools that measure where a program spends its time, and hardware performance counters, which are built-in counters inside the processor, can measure it for a real run.
Related formulas
- Amdahl's Law: Amdahl’s law tells you how much faster a whole program gets when you speed up only part of its work, and why the part you leave alone caps the gain.
- Average Memory Access Time: AMAT is the average time it takes a computer to fetch one piece of data, when most fetches are quick and a few are slow.
- Little's Law: Little’s law says the number of things inside a system equals how fast they arrive, multiplied by how long each one stays.