I was trying to calculate the performance of my algorithm by first implementing it in Java and then calculating the processor time for different inputs. I used ThreadMXBean.getCurrentThreadCpuTime()
to get the system time.
I profiled it first using the following code:
calculateProfile() {
for (int n = 10; n <= 200; n++) {
long time_beg = getCurrentThreadTime();
run_my_algorithm(n);
long time_end = getCurrentThreadTime();
System.out.println("Output = " + n + " " + (time_end - time_beg));
}
However, I was surprised to see that with an increase in the value of n, the time profile does not increase as expected. In some cases, the execution time actually decreased, although my algorithm has a time complexity of O(n).
To look into the problem, I changed this code to run my algorithm once on each execution, and manually checked the time profile for each execution instance. In this case, I find a linear increase in execution time with respect to n. So my question is: How are the two cases different? What is making this difference in profile patterns? And, how to best automate this profiling process in that case?