Skip to content

Instantly share code, notes, and snippets.

@drewhoener
Last active September 21, 2016 16:49
Show Gist options
  • Save drewhoener/1243578275e5fd712bda65ec2b99545c to your computer and use it in GitHub Desktop.
Save drewhoener/1243578275e5fd712bda65ec2b99545c to your computer and use it in GitHub Desktop.
public class BitTests {
public static void main(String[] args) {
new BitTests().run();
}
void run(){
long runOne = 0;
long runTwo = 0;
for(long l = 0; l < Math.pow(10, 8); l++) {
long nanoStart = System.nanoTime();
for (int i = 1; i < Integer.MAX_VALUE; i++) {
int j = i >> 2;
}
runOne += (System.nanoTime() - nanoStart);
nanoStart = System.nanoTime();
for (int i = 1; i < Integer.MAX_VALUE; i++) {
int j = i / 4;
}
runTwo += (System.nanoTime() - nanoStart);
if(l % Math.pow(10, 7) == 0)
System.out.println((l / Math.pow(10, 6)) + "%");
}
System.out.println("\nResults:");
System.out.println("\t" + runOne / Math.pow(10, 8));
System.out.println("\t" + runTwo / Math.pow(10, 8));
}
}
/*
Sample Output Attached:
0.0%
10.0%
20.0%
30.0%
40.0%
50.0%
60.0%
70.0%
80.0%
90.0%
Results:
40.43897064
40.16212578
Timings taken in nanoseconds, so they aren't the most exact they can be
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment