[quote]As for the warmup - I have tried to do it, but making first two calls outside of measuring loop. It turned out to be not enough.
As for the commenting out printline, then it is not fair, because:
- This way you make tested rountine no-op - very bad mistake with modern compilers
[/quote]
Oh is THIS what you were trying to do.
Nice assumption. Totally wrong.
Here are the results for a real noop test.
Results:
Instance method 0ms
Static method 0ms
Instance method 0ms
Code:
package benchmarks;
public class AbiesNoopTest {
public static final int SIZE = 100000000;
public int iadd(int a, int b) {
return a + b;
}
public static int sadd(int a, int b) {
return a+b;
}
public void itest() {
/*
int v = 0;
for ( int i =0; i < SIZE; i++ ) {
int a = iadd(i,1);
v = iadd(v,a);
v = iadd(v,i);
v = iadd(v,5);
v = iadd(v,v);
v = iadd(i,v);
}
// return v;
//System.out.println(v);*/
}
public void stest() {
/*
int v = 0;
for ( int i =0; i < SIZE; i++ ) {
int a = sadd(i,1);
v = sadd(v,a);
v = sadd(v,i);
v = sadd(v,5);
v = sadd(v,v);
v = sadd(i,v);
}
return v;
//System.out.println(v);*/
}
public static void main(String[] argv) {
AbiesNoopTest t = new AbiesNoopTest();
int acc;
t.itest();
t.stest();
long start = System.currentTimeMillis();
for ( int i =0; i < 10; i++ ){
t.itest();
}
System.out.println("Instance method " + (System.currentTimeMillis()-start) + "ms");
start = System.currentTimeMillis();
for ( int i =0; i < 10; i++ ){
t.stest();
}
System.out.println("Static method " + (System.currentTimeMillis()-start) + "ms");
start = System.currentTimeMillis();
for ( int i =0; i < 10; i++ ){
t.itest();
}
System.out.println("Instance method " + (System.currentTimeMillis()-start) + "ms");
}
}
As you can see a REAL noop situation makes this execute almost immediately. I knew this instinctively from all the benchmarks I’ve run in the past, but this proves the point.
In any event a print is the WRONG way to solve that. If the compiler were truly smart enough to figure out that the loop did nothing (which would be quite a feat when you consider that its calling a sub function plus all the possabilities of side-effects) THEN what you should do is return the calculated value rather then void which would take the recognition of whether the value was used or not and mvoe it our of the scope of what you were testing.
NEVER NEVER NEVER do system IO in the midst of a test. I can’t make that point strongly enough.