Sorted arrays and branch predictors
In this post I’ll talk about something every person that has taken classes of data structures will be familiar with. But these days that I’m trying to go deeper in some concepts I’m gonna write about this idea in one particular case: sorted arrays.
The message I got from some those lectures was essentially that something sorted is gonna be faster in most cases than another thing that is unsorted. Well this is certainly true for arrays; if you measure the time it takes to process a sorted array, and compare it with the processing time of an unsorted one, the results will show this idea.
What’s the magic behind this?
We need to go deep to understand why this happens, in fact we need to start thinking in how a processor executes basic instructions. When a processor see a branch (e.g. if clause) it doesn’t know which way it will go.
The processor can halt execution and wait until the previous instructions are completed and then continue down the correct path … or … the processor could guess which direction the branch will go. If the guessing was right it continues executing instructions, other way the processor have to flush the pipeline, roll back to the branch and continue with the other branch.
This is a technology modern processors incorporate and is called Branch Prediction. This technology works by trying to predict which way a branch will go before this is known for sure. Branch predictors try to identify a pattern and follow it.
Most of the time modern branch predictors will guess right because applications use to have well-behaved branches, but when a predictors face unpredictable branches with no recognizable patterns (like in a random array).
Real example
if nums[index] <= 128:
sum += nums[index]
To understand it with an example the code above shows the processing we are doing over an array. When the data is sorted roughly the first half of the iterations will enter the if-statement and immediately after that, they will all enter the if-statement (Suppose the data we generated goes from 0 up to 255). This is really friendly to the branch predictor because the branch consecutively goes the same direction a lot of times.
However, when the data in the array is completely random, the branch predictor is useless because it can't predict random data. This way it will guess right around 50% of the times which is no better than random guessing.
This is the code in the case someone wants to try by himself: Sorted array