Skip to content
systemcoding.dev
Performance

Amdahl's Law and Why Adding Threads Stops Helping

Amdahl's law caps parallel speedup at one over the serial fraction. Work the numbers in C and see why a 95 percent parallel program tops out at 20 times.

6 min read
Summarize in AI
Flat illustration of parallel threads meeting at a mutex, with the C, C++ and Erlang logos.

You parallelise a loop across eight threads and the program gets three times faster, not eight. You go to sixteen threads and it gets a little faster. At thirty-two it barely changes. Nothing is broken. A short formula from 1967 predicts the curve, and once you have seen it, you can look at a program and estimate how far more cores will take it before you write any threaded code. This post states Amdahl's law, works the arithmetic in a small C program, explains what counts as the serial part, and then lists what the law leaves out, because real programs fall short of it for reasons it does not model.

The law in plain terms

Split a program's running time on one core into two parts. A fraction p can be spread across any number of cores, and the remaining fraction 1 - p is serial: it has to happen one step at a time, on one core, whatever else is available. If you run on n cores, the parallel part takes p / n of the original time, and the serial part still takes 1 - p. The total time is (1 - p) + p / n, and the speedup is the original time divided by that:

Text
speedup(n) = 1 / ((1 - p) + p / n)

This is Amdahl's law, from Gene Amdahl's 1967 paper "Validity of the single processor approach to achieving large scale computing capabilities." Take n to infinity and the p / n term vanishes, leaving a ceiling of 1 / (1 - p). A program that is 95 percent parallel can never go faster than 20 times, however many cores you give it, because the serial 5 percent alone takes one twentieth of the original time.

The numbers

Here is a C program that tabulates the speedup and the efficiency, which is speedup divided by core count, for a program that is 95 percent parallel.

amdahl.cc
#include <stdio.h>

static double speedup(double p, double n)
{
    return 1.0 / ((1.0 - p) + p / n);
}

int main(void)
{
    const double p = 0.95;
    const int cores[] = { 1, 2, 4, 8, 16, 64, 1024 };

    printf("parallel fraction %.2f, ceiling %.1f\n", p, 1.0 / (1.0 - p));
    printf("cores  speedup  efficiency\n");
    for (int i = 0; i < 7; i++) {
        double s = speedup(p, cores[i]);
        printf("%5d  %7.2f  %9.1f%%\n", cores[i], s, 100.0 * s / cores[i]);
    }
    return 0;
}
Text
parallel fraction 0.95, ceiling 20.0
cores  speedup  efficiency
    1     1.00      100.0%
    2     1.90       95.2%
    4     3.48       87.0%
    8     5.93       74.1%
   16     9.14       57.1%
   64    15.42       24.1%
 1024    19.64        1.9%

Read the table from the top. Two cores give nearly double. At eight cores you are at 74 percent efficiency, so a quarter of your silicon is waiting on the serial part. At sixteen it is 57 percent, and at 64 cores you are using less than a quarter of the machine. At 1,024 cores, you have bought 19.64 times the speed for 1,024 times the hardware. The returns drop sharply, and the shape is the same for any p, only shifted: a program that is 99 percent parallel has a ceiling of 100, and one that is 50 percent parallel can never reach 2.

What counts as the serial fraction

The 1 - p is not only code written with no threads. It is anything that cannot proceed in parallel.

Setup and teardown count: reading the input file, parsing arguments, allocating memory and writing the result. If the program spends a tenth of its time reading data from a single disk, that tenth is serial. A lock counts. Code inside a critical section that every thread passes through is serial by construction, whatever the thread count. A reduction counts at the end: combining eight partial sums into one is a serial or logarithmic step. Work that depends on a previous result counts, such as each iteration needing the output of the last. And thread creation and joining, barriers and queue hand-offs add serial time that grows with the number of threads, which the simple formula does not capture.

To estimate p for a real program, measure it. Time the program on one core, then time the sections you plan to parallelise. If a profile shows 90 percent of the time in one loop whose iterations are independent, p is up to 0.9, and the ceiling is 10. That figure, computed before writing any parallel code, is worth having: it tells you whether the effort could be worth it. A loop that is 20 percent of runtime has a ceiling of 1.25, and parallelising it perfectly speeds the whole program by a quarter at best.

Inverting the formula

You can run the law backwards to estimate the parallel fraction from a measurement. If a program ran 3.2 times faster on 8 cores, then:

solve_p.cc
#include <stdio.h>

int main(void)
{
    const double s = 3.2, n = 8.0;
    /* s = 1 / ((1 - p) + p / n)  =>  p = (1 - 1/s) / (1 - 1/n) */
    double p = (1.0 - 1.0 / s) / (1.0 - 1.0 / n);
    printf("estimated parallel fraction: %.3f\n", p);
    printf("ceiling at infinite cores: %.2f\n", 1.0 / (1.0 - p));
    return 0;
}
Text
estimated parallel fraction: 0.786
ceiling at infinite cores: 4.67

A 3.2 times speedup on 8 cores implies a parallel fraction of about 78.6 percent and a ceiling of about 4.67, which tells you that sixteen cores will not help much, and that the next step is to attack the serial part. The estimate lumps in every cost, including overhead and contention, so it is a rough number, and it is useful as a check on whether the remaining headroom is in the cores or in the code.

What the law leaves out

Amdahl's law is optimistic in three ways, and one way in the other direction.

It assumes the parallel part scales perfectly. Real parallel code pays for synchronisation, load imbalance, contention on shared data and false sharing. Those make the parallel part slower than p / n, so the measured speedup sits below the curve. If your speedup falls below the Amdahl prediction for your measured p, something in the parallel section is contending, which the other posts in this series look at.

It assumes a fixed problem size. In practice people use more cores to solve a bigger problem, not the same problem faster. John Gustafson pointed out in 1988 that if the parallel work grows with the core count while the serial work stays fixed, speedup keeps growing, and his law gives speedup = n - (1 - p)(n - 1). Both laws are correct, for different questions: Amdahl for how much faster does this job run, Gustafson for how much more can I do in the same time.

It ignores memory. Eight threads that each stream through a large array can saturate the memory bandwidth, and then adding threads adds nothing, long before the serial fraction matters. This shows up as a speedup that flattens at a low core count, with cores sitting idle waiting on DRAM. The two ceilings, serial work and bandwidth, are separate, and either can bind first.

And it ignores hardware reality: hyperthreads share a core's execution units, so sixteen hardware threads on eight cores give far less than double, and turbo frequency drops when many cores are busy.

Using it in practice

Start with a profile, not a thread pool. Find the fraction of time in the code you can parallelise, compute the ceiling, and decide whether it is worth doing. Then measure the speedup at 2, 4 and 8 threads and compare it with the curve for your p. A big gap means contention or imbalance and is worth investigating before you go to more cores. A small gap means you have reached what the structure of the program allows, and the next improvement is to shrink the serial part: overlap input with compute, replace a lock with a sharded structure, or restructure a dependent loop.

Share

Keep reading

Flat illustration of two options weighed side by side, with the C, C++ and Rust logos.
Performance

Spinlocks vs Mutexes: When Spinning Wins

A spinlock burns a core while it waits and a mutex sleeps. Compare their costs, write a correct C11 spinlock, and see when user-space spinning beats a mutex.

6 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.