I have a Java program that calculates an n-body problem. At every iteration it checks the forces each body exerts on every other body, and then moves them according to the forces.
The bodies always start in the same spot (I have them arranged in a circle, from body 0 to body n), they are always inspected and moved in the same order (from body 0 to n). However, when I run the program 30 times, I get drastically different running times. One running time would be 2,947,188 milliseconds (49 minutes), while another would be 920,967 milliseconds (15 minutes). I'm not surprised by the order of magnitude of these times because I'm using brute force method (O(n^2)) on A LOT of bodies. But I'm wondering why is there such a variance for a deterministic algorithm? if it's the same algorithm time after time, shouldn't the running time be the same (or at least close)?
Before you ask, yes, I'm measuring the time of the thread that does the calculation, not wall-clock time.
Edit - I'm measuring time like this:
ThreadMXBean bean = ManagementFactory.getThreadMXBean();
long startUserTimeNano = bean.getCurrentThreadCpuTime();
// ... Code to do the stuff...
double taskUserTimeNano = (bean.getCurrentThreadCpuTime() - startUserTimeNano);
CPUmillisecondsElapsed += taskUserTimeNano/1000000.0;
Does this measure anything other than the calculation step?
Second Edit - Now I changed it to measure time like this:
ThreadMXBean bean = ManagementFactory.getThreadMXBean();
long startUserTimeNano = bean.getCurrentThreadUserTime();
// ... Code to do the stuff...
double taskUserTimeNano = (bean.getCurrentThreadUserTime() - startUserTimeNano);
CPUmillisecondsElapsed += taskUserTimeNano/1000000.0;
However, the results are still not repeatable. I also tried to run my program with the flag -Xint, and the results were STILL not repeatable.
Is it safe to assume the issue is within the algorithm and multithreading? or can it still be an issue relating to Java?