Analysis of Branch Misses in Quicksort

The analysis of algorithms mostly relies on counting classic elementary operations like additions, multiplications, comparisons, swaps etc. This approach is often sufficient to quantify an algorithm's efficiency. In some cases, however, features of modern processor architectures like pipelined execution and memory hierarchies have significant impact on running time and need to be taken into account to get a reliable picture. One such example is Quicksort: It has been demonstrated experimentally that under certain conditions on the hardware the classically optimal balanced choice of the pivot as median of a sample gets harmful. The reason lies in mispredicted branches whose rollback costs become dominating. In this paper, we give the first precise analytical investigation of the influence of pipelining and the resulting branch mispredictions on the efficiency of (classic) Quicksort and Yaroslavskiy's dual-pivot Quicksort as implemented in Oracle's Java 7 library. For the latter it is still not fully understood why experiments prove it 10% faster than a highly engineered implementation of a classic single-pivot version. For different branch prediction strategies, we give precise asymptotics for the expected number of branch misses caused by the aforementioned Quicksort variants when their pivots are chosen from a sample of the input. We conclude that the difference in branch misses is too small to explain the superiority of the dual-pivot algorithm.

[1]  Ronald L. Graham,et al.  Concrete Mathematics, a Foundation for Computer Science , 1991, The Mathematical Gazette.

[2]  C. SIAMJ. OPTIMAL SAMPLING STRATEGIES IN QUICKSORT AND QUICKSELECT , 2001 .

[3]  H. Mahmoud Sorting: A Distribution Theory , 2000 .

[4]  David Gregg,et al.  An experimental study of sorting and branch prediction , 2008, JEAL.

[5]  Salvador Roura,et al.  Improved master theorems for divide-and-conquer recurrences , 2001, JACM.

[6]  L. Devroye Non-Uniform Random Variate Generation , 1986 .

[7]  C. A. R. Hoare Algorithm 63: partition , 1961, CACM.

[8]  Robert Sedgewick,et al.  Implementing Quicksort programs , 1978, CACM.

[9]  Alejandro López-Ortiz,et al.  Multi-Pivot Quicksort: Theory and Experiments , 2014, ALENEX.

[10]  Herbert A. David,et al.  Order Statistics, Third Edition , 2003, Wiley Series in Probability and Statistics.

[11]  Pascal Hennequin Analyse en moyenne d'algorithmes, tri rapide et arbres de recherche , 1991 .

[12]  Peter Sanders,et al.  How Branch Mispredictions Affect Quicksort , 2006, ESA.

[13]  Sebastian Wild,et al.  Average Case Analysis of Java 7's Dual Pivot Quicksort , 2012, ESA.

[14]  Sebastian Wild,et al.  Pivot Sampling in Dual-Pivot Quicksort , 2014 .

[15]  Sebastian Wild,et al.  Java 7's Dual-Pivot Quicksort , 2014 .

[16]  Gerth Stølting Brodal,et al.  Tradeoffs Between Branch Mispredictions and Comparisons for Sorting Algorithms , 2005, WADS.

[17]  Sebastian Wild,et al.  Average Case and Distributional Analysis of Dual-Pivot Quicksort , 2013, ACM Trans. Algorithms.