
30 Jun
2007
30 Jun
'07
2:22 p.m.
Mathias Gaunard wrote:
Lubomir Bourdev wrote:
(Assuming the compiler reduces a switch statement of consecutive integers to constant time dispatch).
That doesn't seem to be the case on the implementations I tried.
Try timing it. On modern CPUs, mispredicted branches affect the speed considerably. If indirect calls are never predicted, a jump table may turn out to be slower. What's even more interesting is that a plain linear search via if( x == 0 ) { ... } else if( x == 1 ) { ... } else if... may outperform a binary search because the branches predict better (there is a break-even N, of course, it's just higher than what we think).