class blah {
public static void main(String[] args) {
System.out.println("Starting timing of modulo version. \n");
long startTime = System.nanoTime();
modulofunc();
long endTime = System.nanoTime();
long duration = endTime - startTime;
System.out.println("Modulo version took " + duration + ". \n");
System.out.println("Starting timing of and version. \n");
startTime = System.nanoTime();
andfunc();
endTime = System.nanoTime();
duration = endTime - startTime;
System.out.println("And version took " + duration + ". \n");
System.out.println("Starting timing of int version. \n");
startTime = System.nanoTime();
intfunc();
endTime = System.nanoTime();
duration = endTime - startTime;
System.out.println("Int version took " + duration + ". \n");
System.out.println("Starting timing of long version. \n");
startTime = System.nanoTime();
longfunc();
endTime = System.nanoTime();
duration = endTime - startTime;
System.out.println("Long version took " + duration + ". \n");
}
static void modulofunc() {
int s = 32;
int e;
for( int i = 0; i < 1_000_000_000; i++) {
e = s % 7;
}
}
static void andfunc() {
int s = 32;
int e;
for( int i = 0; i < 1_000_000_000; i++) {
e = s & 7;
}
}
static void intfunc() {
int s = 32;
int e;
for( int i = 0; i < 1_000_000_000; i++) {
e = s & 7;
}
}
static void longfunc() {
int s = 32;
int e;
for( long i = 0; i < 1_000_000_000L; i++) {
e = s & 7;
}
}
}
Friday, November 2, 2012
Microbenchmarking on the JVM and Long vs Int
While watching a video on lock free hashtables I noticed that you can use & instead of % and gain a performance bump. So I did some benchmarks. Turns out that with modern hardware (I am using a core i5) that the difference between & and modulo wasn't that big. What I found interesting though was when I tried to up the number of loop iterations I had to switch to a long which made a large difference. Seems that even on a 64bit architecture with a 64bit JVM a loop with a long as the iteration variable was significantly slower. Any ideas?
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment