Skip to content

Instantly share code, notes, and snippets.

@sodik82
Created February 3, 2015 09:12
Show Gist options
  • Save sodik82/d7525a8a20e8855461b3 to your computer and use it in GitHub Desktop.
Save sodik82/d7525a8a20e8855461b3 to your computer and use it in GitHub Desktop.
package sk.sodik.perf;
public class BitVsDiv {
public static void main(String[] args) {
long start1 = System.nanoTime();
int result1 = divide(1000000);
long duration1 = System.nanoTime() - start1;
long start2 = System.nanoTime();
int result2 = bit(1000000);
long duration2 = System.nanoTime() - start2;
System.out.println("Results: " + result1 + " and " + result2);
System.out.println("Times: " + duration1 + " and " + duration2);
}
private static int bit(int rounds) {
int sum = 0;
for (int i = 0; i < rounds; i++) {
for (int j = 0; j < 10000; j++) {
sum += j >> 1;
}
}
return sum;
}
private static int divide(int rounds) {
int sum = 0;
for (int i = 0; i < rounds; i++) {
for (int j = 0; j < 10000; j++) {
sum += j / 2;
}
}
return sum;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment