You may be a victim branch prediction!

background

A length N = 1000000 conventional array a [N], each element in the range 0-255. Require all elements of less than 128 is set to 0, the element set 128 is greater than or equal to 1

It is easy to write such code to iterate

for (i = 0; i < N; i++) {
    if (a[i] < 128) {
        a[i] = 0;
    } else {
        a[i] = 1;
    }
}

Think about a problem, a disorderly and orderly array, speed of execution of this code will affect you? At first glance it does not seem to, but the actual execution time may differ 3-4 times.

Why is through the array to determine the elements. Traversing an ordered array of speed will be several times faster than a disordered array it?

You may be a victim branch prediction!

train

Consider a train turnoff, an administrator needs to toggle the joystick left or right to make the train move.
But the administrators do not want the train to come to know in which direction to move forward. That there are two methods:

1, the train stopped reaching the intersection, it wants to tell the administrator the way forward, the administrator toggle levers, the train restarted. It takes five minutes

In this case, the benefits of the train never the wrong direction, the downside is that each train needs to stop and restart

2, before the train reached the turnoff, the administrator guess a random direction.
(2-1) If you guessed it, no need to slow down the train, direct peers, takes 0 minutes
(2-2) If you guess wrong, the train driver will Mamalielie reversing and re heading in the right direction, took 10 minute

Consider two extreme cases: Administrator always guess right, then the train traffic efficiency will become very high
administrator is always wrong: the train counterparts efficiency becomes abnormally low (which also run bad)

Normal circumstances: An administrator will always have a 50% chance or guess wrong, time consuming and a little different scheme.
Administrators do something just a little summary and reflection, such as train 16:00, and left to go 19 times, walked once to the right, then the left guessing, we can increase the probability of guessing

Modern CPU

Similar examples of modern CPU and above the train. The train is a command, the administrator is the CPU. For the branch instruction if the like, CPU does not know in advance which branch will go.
Modern CPU tend to have a multi-stage pipeline for processing instructions, if the needs of each command card statements at the branch, waiting for conditions to determine complete then go down, efficiency will be low

So the CPU will first branch to predict when the code has not come to the branch, the result of a guess in advance and executed down

If you guess right, there is no obstruction to continue, if they guess wrong, put clean out all the instructions, reprocessed.

Iterator branch predictor

The train back to just examples, administrator forecast a train going to the left or the right, if it is completely random, so there is a 50% chance he guessed, a 50% chance mistaken, not doing much to save time.

A relatively simple idea is that if left on a train, then a guess that also left.

Using this strategy, shares more than a continuous trains are heading in the same direction, then the access speed will become very fast.

Other more advanced algorithms, often retains this idea.

Back to the code

So, if the array is a sort of advance, branch predictor can be a very good job

Array 123 .... .... 126,127,128,129,130
guess TTT ..... TTTFF ....
actual TTT ..... TTFFF ....

Branch predictor wrong only once! (Train only reverse once, other times it can quickly pass)

If the array is disordered words

Array 11713371401899 ....
guess TTTFTFF
actual TTFTFFT

So the chance to guess becomes low (more trains require reversing)

Of course, the iterative branch predictor is the simplest model, actual prediction model will be more complicated to ensure higher rates guess

The next article will show you how to avoid becoming a branch prediction by advanced programming skills victims

Guess you like

Origin www.cnblogs.com/velscode/p/12585171.html