Skip to content

Instantly share code, notes, and snippets.

@mikeb01
Created August 30, 2012 10:46
Show Gist options
  • Save mikeb01/3526133 to your computer and use it in GitHub Desktop.
Save mikeb01/3526133 to your computer and use it in GitHub Desktop.
PopCnt Benchmark
public class PopCntBenchmark extends SimpleBenchmark
{
public int timeIntegerBitCount(int iterations)
{
int result = 0;
for (int i = 0; i < iterations; i++)
{
result += Integer.bitCount(i);
}
return result;
}
public int timeMyBitCount(int iterations)
{
int result = 0;
for (int i = 0; i < iterations; i++)
{
result += myBitCount(i);
}
return result;
}
private static int myBitCount(int i)
{
// HD, Figure 5-2
i = i - ((i >>> 1) & 0x55555555);
i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
i = (i + (i >>> 4)) & 0x0f0f0f0f;
i = i + (i >>> 8);
i = i + (i >>> 16);
return i & 0x3f;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment