Reputation: 71
I just worte a programm in java to test the time spent in executing myQsort.
part of the code looks like this:
long t1 = System.currentTimeMillis();
new sort().myQsort(a, 0, 100000 - 1);
long t2 = System.currentTimeMillis();
I loop the statements five times to get five timeSpent (t2-t1) and print them out, but the the results are strange, everytime the first timeSpent appears lager than the rest
Quick Sort in Java:
Round1: 30ms
Round2: 9ms
Round3: 9ms
Round4: 11ms
Round5: 9ms
Average time spent: 13ms
I have tried reading the 5 input files in a different order, or even reading the same file for 5 times. But the results are all similar. I also tried running the code on a different computer, while the result is still the same.
Can anyone explain why this happened?
Question Solved
Thanks for all the answers, I've tried add -Xint to the VM settings (inorder to shut down the JIT), and this time everying works pretty well.
Quick Sort in Java:
Round1: 61ms
Round2: 68ms
Round3: 72ms
Round4: 73ms
Round5: 59ms
Average time spent: 66ms
Thanks again for all your answers, it helps a lot :)
Upvotes: 1
Views: 65
Reputation: 2221
Apart from JIT compilation, another factor may be the state of your list to be sorted. Just like Quicksort performs worst if the list is already sorted, your sorting algorithm may perform best if the list is already sorted.
So, you may want to make sure that your sorting algorithm did not change the state of the list or you need to make copies of your list before starting your tests.
Upvotes: 0
Reputation: 476669
The first time you run it, this is a cold start, meaning that Java loads in the code of the program and its own code for the first time.
The second time it is already in memory. As you can see the time does not vary that much after the first time.
Furthermore milliseconds may vary by events that happen. For instance something is send to you through the network, your OS needs to modify an important table, etc.
Upvotes: 4