I tried to find execution time of two methods in java. both the method doing same functionality but different logic, the execution time differs in lot. then i tried to find the execution time of same method two time, but the execution time are not even almost equal differs by multiples of 10's. so please advise me, how measure the exact execution time of java method. `
public static void main(String[] args) throws Throwable {
double t11 = System.nanoTime();
System.out.println(test(2, 32));
double t12 = System.nanoTime();
System.out.println("Total time taken : "+(t12-t11));
double t1 = System.nanoTime();
System.out.println(test(2, 32));
double t2 = System.nanoTime();
System.out.println("Total time taken : "+(t2-t1));
}
public static long test(int num1, int num2){
long value = num1;
for(int i=2; i<=num2; i++) {
value *= num1;
}
return value;
}
Output :
4294967296
Total time taken : 207084.0
4294967296
Total time taken : 35621.0