3 ms·
Here is a more exhaustive test that throws in a random boolean array (bb) for a total mispredict scenario (always read, sometimes used) First, modulo oscillati
by seanmcdirmid 11y ago
Here is a more exhaustive test that throws in a random boolean array (bb) for a total mispredict scenario (always read, sometimes used)
First, modulo oscillation:
var b = true;
for (var i = 0; i < x.Length; i += 1)
{
var cc = bb[i];
if (b)
x[i] = 0;
else
x[i] = 1;
b = !b;
}
Time: 36 milliseconds (averaged over 10 runs)
Case 2, no branch:
for (var i = 0; i < x.Length; i += 2)
{
var c = bb[i];
x[i] = 0;
}
for (var i = 1; i < x.Length; i += 2)
{
var c = bb[i];
x[i] = 1;
}
Time: 22 milliseconds
Case 3, coherent branching:
var kk = x.Length / 2;
for (var i = 0; i < x.Length; i += 1)
{
var c = bb[i];
if (i < kk)
x[i] = 0;
else x[i] = 1;
}
Time: 31 milliseconds
Case 4, totally random branching:
for (var i = 0; i < x.Length; i += 1)
if (bb[i])
x[i] = 0;
else x[i] = 1;
Time: 75 milliseconds (the slowness could come from bad branch prediction, or the fact that a load needs to complete before the condition can be verified!)
- caf 11y agoMy point is just to reiterate that the 0,1,0,1,0,1,... pattern of i % 2 == 0 is actually exactly the kind of pattern that branch predictors excel at, which you seemed to be skeptical of.
- seanmcdirmid 11y agoRight, I was wrong. I'm wondering what the use case is for that kind of silicon... interpreters?