Abstract
According to a folk theorem, every program can be transformed into a program that produces the same output and only has one loop. We generalize this to a form where the resulting program has one loop and no other branches than the one associated with the loop control. For this branch, branch prediction is easy even for a static branch predictor. If the original program is of length κ, measured in the number of assembly-language instructions, and runs in t(n) time for an input of size n, the transformed program is of length O(κ) and runs in O(κt(n)) time. Normally sorting programs are short, but still κ may be too large for practical purposes. Therefore, we provide more efficient hand-tailored heapsort and mergesort programs. Our programs retain most features of the original programs—e.g. they perform the same number of element comparisons—and they induce O(1) branch mispredictions. On computers where branch mispredictions were expensive, some of our programs were, for integer data and small instances, faster than the counterparts in the GNU implementation of the C++ standard library.
Access this chapter
Tax calculation will be finalised at checkout
Purchases are for personal use only
Preview
Unable to display preview. Download preview PDF.
Similar content being viewed by others
References
Aho, A.V., Lam, M.S., Sethi, R., Ullman, J.D.: Compilers: Principles, Techniques, & Tools, 2nd edn. Pearson Education, Inc., Boston (2007)
Biggar, P., Nash, N., Williams, K., Gregg, D.: An experimental study of sorting and branch prediction. ACM J. Exp. Algorithmics 12, Article 1.8 (2008)
Brodal, G., Moruz, G.: Tradeoffs Between Branch Mispredictions and Comparisons for Sorting Algorithms. In: Dehne, F., López-Ortiz, A., Sack, J.-R. (eds.) WADS 2005. LNCS, vol. 3608, pp. 385–395. Springer, Heidelberg (2005)
Floyd, R.W.: Algorithm 245: Treesort 3. Commun. ACM 7(12), 701 (1964)
Harel, D.: On folk theorems. Commun. ACM 23(7), 379–389 (1980)
Katajainen, J., Träff, J.L.: A Meticulous Analysis of Mergesort Programs. In: Bongiovanni, G., Bovet, D.P., Di Battista, G. (eds.) CIAC 1997. LNCS, vol. 1203, pp. 217–228. Springer, Heidelberg (1997)
Kernighan, B.W., Ritchie, D.M.: The C Programming Language, 2nd edn. Prentice Hall, Englewood Cliffs (1988)
Mortensen, S.: Refining the pure-C cost model. Master’s Thesis, Department of Computer Science. University of Copenhagen, Copenhagen (2001)
Patterson, D.A., Hennessy, J.L.: Computer Organization and Design, The Hardware/Software Interface, 4th edn. Morgan Kaufmann Publishers, Burlington (2009)
Sanders, P., Winkel, S.: Super Scalar Sample Sort. In: Albers, S., Radzik, T. (eds.) ESA 2004. LNCS, vol. 3221, pp. 784–796. Springer, Heidelberg (2004)
Sedgewick, R.: Algorithms in C++, Parts 1–4: Fundamentals, Data Structures, Sorting, Searching, 3rd edn. Addison-Wesley Publishing Company, Inc., Reading (1998)
Williams, J.W.J.: Algorithm 232: Heapsort. Commun. ACM 7(6), 347–348 (1964)
Author information
Authors and Affiliations
Editor information
Editors and Affiliations
Rights and permissions
Copyright information
© 2012 Springer-Verlag Berlin Heidelberg
About this paper
Cite this paper
Elmasry, A., Katajainen, J. (2012). Lean Programs, Branch Mispredictions, and Sorting. In: Kranakis, E., Krizanc, D., Luccio, F. (eds) Fun with Algorithms. FUN 2012. Lecture Notes in Computer Science, vol 7288. Springer, Berlin, Heidelberg. https://doi.org/10.1007/978-3-642-30347-0_14
Download citation
DOI: https://doi.org/10.1007/978-3-642-30347-0_14
Publisher Name: Springer, Berlin, Heidelberg
Print ISBN: 978-3-642-30346-3
Online ISBN: 978-3-642-30347-0
eBook Packages: Computer ScienceComputer Science (R0)