Definition
Amdahl’s law is a formula for the overall speedup §18.1.13 of a task when only one part of the task is made faster. Speedup is the old time divided by the new time. Let be the fraction of the original time spent in the part that is improved, and let be how many times faster that part becomes. The whole task then becomes times faster. The unimproved fraction caps the overall speedup at , however large is.
The idea
A detector is a small program that examines each event a system reports and decides whether the event breaks a rule. The detector is stored as bytecode §2.4, a compact instruction format for a virtual machine §2.3 (VM): a program that carries out such instructions instead of the processor running them directly. An interpreter §2.13, the simplest way to build a VM, fetches each bytecode instruction, decodes it and then carries it out. Compiling the detector to native code, the processor’s own machine instructions, removes the fetching and decoding. Suppose the compiled instructions run four times faster than the interpreted ones. The tempting conclusion is that every call that runs the detector becomes four times faster.
The conclusion fails because a call does more than run the detector’s instructions. The system first builds the context, the record of event data that the detector reads; the detector calls helper functions §2.19, routines of the host program that are native code already; and the system then acts on the verdict. Compilation speeds up none of that work, so the gain for the whole call is limited by the share of time that the detector’s own instructions took.
A trip is a fair picture: 2 hours of driving to and from airports plus 8 hours of flying. A plane four times faster cuts the trip to hours, a speedup of , not 4. Even an instant flight leaves the 2 hours of driving, so no plane makes the trip more than times faster. The picture stops being accurate where the parts interact. In a computer, speeding up one part can change the time of the others, for example by changing what the processor’s caches hold.
How it works
The calculation needs measured times, not counts of instructions or lines of code.
Fix the endpoints: the event that starts the measured interval and the event that ends it, for example the entry into the callback §14.22, the function the system calls for each event, and the return from it.
Split the baseline time of that interval into the time of the part the change accelerates and the time of everything else, so that . Measure both on the same workload, meaning the same inputs.
Compute .
Measure , the speedup of the accelerated part alone: its old time divided by its new time.
Measure any extra time that the change adds to every call.
The new time is , and the overall speedup is .
One call of a detector that a VM runs has the following parts, and compilation accelerates only the instruction execution.
Context building, entry, exit and result handling: preparing the detector’s input, starting it, collecting its result and acting on the verdict.
Instruction execution: the detector’s own operations, including each runtime bounds check §2.27, a check made while the detector runs that a memory access stays inside the permitted region. An interpreter spends much of this time fetching, decoding and dispatching instructions, where dispatching means jumping to the code that handles each kind of instruction; compiled code does none of that. Compiled code must still make every bounds check that was not proved unnecessary before the detector ran, so those checks become cheaper but do not disappear.
Helper calls: helper bodies are native code already and take the same time.
Gene Amdahl made this argument in words in 1967, about computers with many processors. There the improved part is the work that can be spread over processors, so at best . The formula is the standard later statement of his argument; the 1967 paper itself contains no equation.
Worked example
The numbers are invented. One call of the callback takes s (microseconds) when the detector is interpreted. A timing breakdown splits the call into three segments:
setup, which builds the context: 1.0 s;
the VM segment, from entering the VM to leaving it: 8.6 s, of which 8.0 s is interpreted instruction execution and 0.6 s is time inside helper function bodies;
result handling: 0.4 s.
Check: s.
Compilation accelerates only the 8.0 s of instruction execution, so . Let the compiled instructions run times faster, taking s. The VM segment becomes s, not zero. The whole call becomes s, and the overall speedup is . The formula gives the same result: .
The cap. If the compiled instructions took no time at all, the call would still take s. The speedup can therefore never exceed , which equals .
Added costs. If the compiled version adds 1.0 s of new work to every call, the call takes s, and the speedup falls to . Compiling also has a one-time cost. Let a just-in-time compiler §2.14, which translates bytecode to native code while the system runs, translate the detector once, when it is loaded. If that takes 300 s and nothing is added per call, each call saves s, and compiling pays for itself after calls. A detector that runs fewer than 50 times before it is replaced costs more time compiled than interpreted.
Stated precisely
Let be the baseline time of one measured interval, written as , where is the time of the part a change accelerates and is the time of everything else. Let , let be the speedup of the accelerated part (its old time divided by its new time), and let be the extra time the change adds to the interval. The new time and the overall speedup are With this is Amdahl’s law. For , the overall speedup never exceeds , whatever is.
The formula is exact when three assumptions hold.
Additivity: the parts run one after another and do not overlap, so the time of the interval is the sum of the times of its parts.
Unchanged remainder: the change leaves as it was. It does not alter cache contents, memory layout or scheduling in a way that changes the other parts.
Same workload and endpoints: , and are measured with the same inputs and the same start and end events as the claim they support.
Amdahl’s law keeps the amount of work fixed and asks how much sooner it finishes. Gustafson’s law (1988) [408] asks instead how much more work a parallel computer finishes in the same time when processors are added.
The equation holds for each single call, and therefore for mean times, because the mean of a sum equals the sum of the means. It does not hold for medians §11.13: the median of a sum is in general not the sum of the medians of its parts. A breakdown that stacks the median time of each segment therefore need not add up to the median time of the whole call.
Limits and common mistakes
Amdahl’s law is bookkeeping, not a prediction. It is exact only while its three assumptions hold, and a real change can alter the parts the formula treats as fixed. Compiled code, for example, occupies memory of its own and can change what the processor’s caches hold for the other parts.
The fraction is a fraction of time on one workload, not a fraction of code. A detector that makes up 90% of the code may take 30% of the time. A detector that rejects most events after a few instructions has a small and gains little from compilation. A speedup measured on one workload does not carry over to another.
A figure that shows only the detector’s own time can show , the speedup of the accelerated part. It cannot by itself show , the speedup of the whole call, because it leaves out .
A faster call does not mean more events handled per second. When another stage, such as a radio link, limits that rate, a faster callback leaves it unchanged; the computational versus communication bottleneck §14.37 entry treats that case.
Questions answered
Does compiling a detector to native code remove the entire VM segment?
No, compiling a detector to native code does not remove the entire VM segment. The VM (virtual machine) segment of a timing breakdown is the time from entering the VM to leaving it. Compiling removes only the overhead of the interpreter, the part of the VM that carries out the detector’s bytecode, its stored instructions, one at a time: the work of fetching, decoding and dispatching each instruction. That makes the detector’s own operations cheaper. The segment still contains the compiled instructions, which take time; the bodies of helper functions, the host program’s routines that the detector calls, which were native code already; every safety check that was not proved unnecessary in advance; and the work of entering and leaving the detector. Work outside the segment, such as building the detector’s input and acting on its verdict, does not change at all.
Amdahl’s law gives the size of the gain: if the part that compiling speeds up took a fraction of the original time and becomes times faster, the whole call becomes times faster. In an invented example, a 10 s (microsecond) call has an 8.6 s VM segment holding 8.0 s of interpreted instructions and 0.6 s of helper bodies. With , the VM segment shrinks to s, not zero, and the call to 4.0 s, a speedup of 2.5.
Related entries: Median (§11.13), Computational versus communication bottleneck (§14.37), Virtual machine (VM) (§2.3), Interpreter (§2.13), Just-in-time compilation (JIT) (§2.14), Contrast, normalization, ratio, and percent overhead (§18.1.13), The cost of interpreter versus JIT execution (§17.6). Further reading: [184, 408].