Table 8.1 shows the relative effectiveness of the various improvements that we have examined. As is often the case, these studies
In addition to netting the improvements discussed in Section 8.2, a good Java VM implementation might avoid unnecessary array
|
These relative timings for various sorts on random files of integers indicate that standard quicksort is about twice as fast as standard mergesort; that adding a cutoff for small files lowers the running times of both bottom-up and top-down mergesort by about 15 percent; that top-down mergesort is about 10 percent faster than bottom-up mergesort for these file sizes; and that even eliminating the cost of the file copy (the method used in the Java system
|
|||||||
|
top-down |
bottom-up |
||||||
|
N |
Q |
T |
T* |
O |
B |
B* |
S |
|
12500 |
23 |
27 |
16 |
19 |
30 |
20 |
19 |
|
25000 |
20 |
43 |
34 |
27 |
42 |
36 |
28 |
|
50000 |
45 |
91 |
79 |
52 |
92 |
77 |
56 |
|
100000 |
95 |
199 |
164 |
111 |
204 |
175 |
117 |
|
200000 |
201 |
421 |
352 |
244 |
455 |
396 |
256 |
|
400000 |
449 |
904 |
764 |
520 |
992 |
873 |
529 |
|
800000 |
927 |
1910 |
1629 |
1104 |
2106 |
1871 |
1119 |
|
Key : Q Quicksort, standard (Program 7.1) T Top-down mergesort, standard (Program 8.1) T* Top-down mergesort with cutoff for small files O Top-down mergesort with cutoff and no array copy B Bottom-up mergesort, standard (Program 8.5) B* Bottom-up mergesort with cutoff for small files S java.util.Arrays.sort |
|||||||
As usual, we must add the caveat that pursuit of improvements of this nature, although irresistible to many programmers, can sometimes lead to marginal gains and should be taken on only after more important considerations have been resolved. In this case, mergesort has the clear advantages over quicksort that it is stable and is
On the other hand, we must also add the usual caveat that programmers should always have one eye on performance in order to avoid costs that are completely unnecessary. All programmers (and authors!) have suffered the embarrassment of having a simple unnoticed characteristic of an implementation dominate all that implementation's other sophisticated mechanisms. It is not unusual for a factor-of-2 improvement in running time to be found when
We discussed these points at length in Chapter 5, but the allure of premature optimization is so strong that it is worthwhile to
The running time for mergesort is insensitive to the input. These diagrams
8.31 Implement bottom-up mergesort with no array copy.
8.32 Develop a
three-level hybrid sort that uses quicksort, mergesort, and insertion sort to get a method that is as fast as the most efficient quicksort (even on small files) but can guarantee better than quadratic performance in the worst case.
| Top |