I made a little benchmark program and since im a little rookie in this stuff i have some doubts on how to interpret the results.
The program is this:
public class Tests {
public static void main(String args[]) {
System.out.println("\ntest0 - currentTimeMillis precision");
test0(); test0();
System.out.println("\ntest1 - empty cycle");
test1(); test1(); test1(); test1();
System.out.println("\ntest2 - local mul");
test2(); test2(); test2(); test2();
System.out.println("\ntest3 - static mul");
test3(); test3(); test3(); test3();
System.out.println("\ntest4 BC*4 - instance variable access");
test4(); test4(); test4(); test4();
System.out.println("\ntest5 - instance variable mul");
test5(); test5(); test5(); test5();
System.out.println("\ntest6 BC/20 - instance creation");
test6(); test6(); test6(); test6();
test6(); test6(); test6(); test6();
}
// A benchmark cycle
public static int BC = 10000000;
// the precision is P
public static long P = 0;
public static void test0() {
// reset P
P = 0;
// indexes 0 and last are to be discarded later
int TIME_ARRAY = 7;
long n[] = new long[TIME_ARRAY];
n[0] = System.currentTimeMillis();
for (int i=1; i < TIME_ARRAY; i++) {
while (n[i-1] == System.currentTimeMillis()) ;
n[i] = System.currentTimeMillis();
}
// Only use indexes from 1 to preceding last index
// (remenber valid indexes go from 0 to TIME_ARRAY-1)
for (int i=1; i < TIME_ARRAY-1; i++) {
long x = n[i]-n[i-1];
P += x;
System.out.print(x + " ");
}
// average it
System.out.println("\nTotal: " + P);
P /= TIME_ARRAY-2;
System.out.println("Precision: " + P);
}
public static void test1() {
long start = System.currentTimeMillis();
for (int i=0; i < BC; i++) ;
long end = System.currentTimeMillis();
long frame = (end - start);
System.out.print(frame + "-" + frame/P + " ");
}
public static void test2() {
int x=0;
long start = System.currentTimeMillis();
for (int i=0; i < BC; i++) x = x * 2;
long end = System.currentTimeMillis();
long frame = (end - start);
System.out.print(frame + "-" + frame/P + " ");
}
public static int x;
public static void test3() {
long start = System.currentTimeMillis();
for (int i=0; i < BC; i++) x = x * 2;
long end = System.currentTimeMillis();
long frame = (end - start);
System.out.print(frame + "-" + frame/P + " ");
}
public static void test4() {
long start = System.currentTimeMillis();
CTest ct = new CTest();
ct.self = ct;
Object ref = null;
for (int i=0; i < BC; i++) ref = ct.self.self.self.self;
long end = System.currentTimeMillis();
long frame = (end - start);
System.out.print(frame + "-" + frame/P + " ");
}
public static void test5() {
long start = System.currentTimeMillis();
CTest ct = new CTest();
for (int i=0; i < BC; i++) ct.x = ct.x * 2;
long end = System.currentTimeMillis();
long frame = (end - start);
System.out.print(frame + "-" + frame/P + " ");
}
public static void test6() {
long start = System.currentTimeMillis();
CTest ct = null;
Object ref = null;
for (int i=0; i < BC/20; i++) ref = new CTest();
long end = System.currentTimeMillis();
long frame = (end - start);
System.out.print(frame + "-" + frame/P + " ");
}
}
class CTest {
public int x;
public CTest self;
}
And this is the result on a 1ghz cpu running jdk1.4.2
test0 - currentTimeMillis precision
10 10 10 10 10
Total: 50
Precision: 10
10 10 10 10 10
Total: 50
Precision: 10
test1 - empty cycle
40-4 40-4 40-4 30-3
test2 - local mul
60-6 61-6 60-6 70-7
test3 - static mul
50-5 60-6 50-5 60-6
test4 BC*4 - instance variable access
110-11 110-11 121-12 120-12
test5 - instance variable mul
50-5 50-5 50-5 60-6
test6 BC/20 - instance creation
60-6 60-6 40-4 50-5 40-4 50-5 51-5 40-4
According to the results one multiplication operation, on my machine, takes about 5ns and allocating memory for an object takes 20 times more time, that is 100 ns.
Is this a correct interpretation ?
Note: in the values above like 40-4, 40 is the time taken to perform BC operations and 4 is the resulte of dividing it by P