If a CPU sees a line of 10 people, it will brew 10-cups of coffee, speculating that most want coffee. If only 9-cups were needed, it will throw away the extra coffee.
-----------
In practice, this truly happens. CPUs perform branch-prediction over a for-loop.
for(int i=0; i<32; i++){
doA();
}
doB()
The value "i" hasn't been calculated yet, but the CPU performs branch prediction. Modern CPUs can accurately loops of size ~32 or less. Modern CPUs will literally fill their pipelines with 32x "doA()" statements, and even the doB() statement BEFORE the i<32 check was even tested.Now the branch-predictor might be wrong! Lets say that doA() is:
if(random() == 1% chance){
i++; // Increments the loop counter
}
The CPU will likely fail to predict this, and then will be forced to throw away the work. Nonetheless, its overall beneficial for the CPU to speculatively try to do all the loops anyway (the alternative is leaving the CPU-pipeline empty, which has roughly the same costs as a failed speculation anyway).CPU-wins if it is correct, and it ties if it is wrong. So might as well speculate.