I'm trying to record the time elapsed to run a certain method using currentTimeMillis, but am having trouble printing out the time...
public int foo(int n, int k) {
if(n<=k)
return 1;
else
return foo(n/k,k) + 1;
}
Where would I write in the lines for starTime, endTime, and the difference?
I tried throwing it in a different method:
public static long p3(int n, int k){
long starTime = System.currentTimeMillis();
foo(n, k);
long endTime = System.currentTimeMillis();
long difference = endTime - starTime;
return difference;
}
But the difference always appears as 0...
If the method takes less than a milisecond to execute then yes the difference will be zero.
Then you need to work with nanoseconds. System class has a method for these too: System#nanoTime();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.