Skip to content

Instantly share code, notes, and snippets.

@Xophmeister
Created June 28, 2012 13:28
Show Gist options
  • Save Xophmeister/3011398 to your computer and use it in GitHub Desktop.
Save Xophmeister/3011398 to your computer and use it in GitHub Desktop.
Branch Prediction Benchmarking in D
import std.stdio, std.random, core.time;
void main() {
int data[32768];
foreach(ref x; data)
x = uniform(0, 256);
data.sort;
ulong sum;
auto start = TickDuration.currSystemTick();
for(auto i = 0; i < 100000; ++i)
foreach(x; data)
if (x >= 128)
sum += x;
writeln("Done in ", (TickDuration.currSystemTick() - start).msecs(), "ms");
writeln("sum = ", sum);
}
@Xophmeister
Copy link
Author

Inspired by this question on StackOverflow:

Compiled with -O -release under dmd 2.057 (Win32) on Intel Core2 6420 @ 2.13GHz

  • With sorting: 7367ms
  • Without sorting: 20881ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment