I have implemented merge sort in Java. Instance of the class is named ms. rArray is a random integer array. For testing i ran it with the following code:
for (int i=0; i<10; i++) {
System.out.println("Starting Merge Sort");
testArray = rArray; // Create duplicate for testing
long startM = System.currentTimeMillis();
ms.sort(testArray);
long stopM = System.currentTimeMillis();
long durationM = stopM-startM;
System.out.println("Size: " + size + "; Duration: " + durationM + "ms");
System.out.println("++++++++++++++++++++++++++++++++++++++++++");
}
Output:
Starting Merge Sort
Size: 1000000; Duration: 5853ms
++++++++++++++++++++++++++++++++++++++++++
Starting Merge Sort
Size: 1000000; Duration: 4527ms
++++++++++++++++++++++++++++++++++++++++++
Starting Merge Sort
Size: 1000000; Duration: 4082ms
++++++++++++++++++++++++++++++++++++++++++
Starting Merge Sort
Size: 1000000; Duration: 3000ms
++++++++++++++++++++++++++++++++++++++++++
Starting Merge Sort
Size: 1000000; Duration: 2930ms
++++++++++++++++++++++++++++++++++++++++++
Starting Merge Sort
Size: 1000000; Duration: 2904ms
++++++++++++++++++++++++++++++++++++++++++
Starting Merge Sort
Size: 1000000; Duration: 3403ms
++++++++++++++++++++++++++++++++++++++++++
Starting Merge Sort
Size: 1000000; Duration: 3570ms
++++++++++++++++++++++++++++++++++++++++++
Starting Merge Sort
Size: 1000000; Duration: 2930ms
++++++++++++++++++++++++++++++++++++++++++
Starting Merge Sort
Size: 1000000; Duration: 3133ms
It is the same pattern for every run.
Why is it always speeding up? I guess it has to do with the compiler, since Arrays.sort
shows the same behaviour. What i don't understand is, if i first run Arrays.sort
and then my implementation of merge sort, why is the latter still faster compared to if i run my implementation of merge sort on it's own?
Update
After using System.arraycopy(rArray, 0, testArray, 0, rArray.length);
as suggested by Makoto (Thanks!), the output is getting better. But as for Merge sort it remains the same. My implementation of merge sort is not saving the sorting result. So is the reason for this speedup the JIT Warmup?
New Output:
Starting Default Sort
Size: 1000000; Duration: 1799ms
++++++++++++++++++++++++++++++++++++++++++
Starting Default Sort
Size: 1000000; Duration: 1816ms
++++++++++++++++++++++++++++++++++++++++++
Starting Default Sort
Size: 1000000; Duration: 945ms
++++++++++++++++++++++++++++++++++++++++++
Starting Default Sort
Size: 1000000; Duration: 944ms
++++++++++++++++++++++++++++++++++++++++++
Starting Default Sort
Size: 1000000; Duration: 944ms
++++++++++++++++++++++++++++++++++++++++++
Starting Default Sort
Size: 1000000; Duration: 943ms
++++++++++++++++++++++++++++++++++++++++++
Starting Default Sort
Size: 1000000; Duration: 957ms
++++++++++++++++++++++++++++++++++++++++++
Starting Default Sort
Size: 1000000; Duration: 963ms
++++++++++++++++++++++++++++++++++++++++++
Starting Default Sort
Size: 1000000; Duration: 964ms
++++++++++++++++++++++++++++++++++++++++++
Starting Default Sort
Size: 1000000; Duration: 952ms
++++++++++++++++++++++++++++++++++++++++++
Starting Merge Sort
Size: 1000000; Duration: 5913ms
++++++++++++++++++++++++++++++++++++++++++
Starting Merge Sort
Size: 1000000; Duration: 3171ms
++++++++++++++++++++++++++++++++++++++++++
Starting Merge Sort
Size: 1000000; Duration: 3093ms
++++++++++++++++++++++++++++++++++++++++++
Starting Merge Sort
Size: 1000000; Duration: 4816ms
++++++++++++++++++++++++++++++++++++++++++
Starting Merge Sort
Size: 1000000; Duration: 3685ms
++++++++++++++++++++++++++++++++++++++++++
Starting Merge Sort
Size: 1000000; Duration: 3094ms
++++++++++++++++++++++++++++++++++++++++++
Starting Merge Sort
Size: 1000000; Duration: 3488ms
++++++++++++++++++++++++++++++++++++++++++
Starting Merge Sort
Size: 1000000; Duration: 3660ms
++++++++++++++++++++++++++++++++++++++++++
Starting Merge Sort
Size: 1000000; Duration: 3094ms
++++++++++++++++++++++++++++++++++++++++++
Starting Merge Sort
Size: 1000000; Duration: 3604ms
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…