Choosing algorithms and data structures
Choosing an algorithm or data structure means matching the operations a program needs and the inputs it will see to an implementation, then measuring it. Asymptotic complexity, the big-O bound on how work grows with input size, predicts how candidates scale. Measured speed also depends on constant factors, the memory hierarchy and setup costs (Chimani & Klein, 2010; LaMarca & Ladner, 1999; Liu & Vinter, 2015). Three questions remain open: whether cache-oblivious designs pay off, when a model can replace timing, and how well benchmarks measure the speed of generated code. The specific families have their own articles: sorting and searching, hash tables, search trees and ordered indexes, probabilistic data structures, graph algorithms, concurrent data structures, string algorithms and cache eviction.
Starting from the operations
A problem is defined by the operations it must support, and problems that look alike can call for different structures. Reference catalogues index problems by their input and desired result. Skiena’s repository sorts 75 problems into five groups and cross-links related formulations (Skiena, 1999). Introduction to Algorithms likewise treats single-source and all-pairs shortest paths as separate problems (Cormen et al., 2022).
Whether the data changes after the first query is a basic dividing line. Searching a fixed set permits simpler and more efficient solutions than a dictionary, a structure that keeps records keyed for lookup, insertion and deletion (Skiena, n.d.d; Skiena, n.d.a). In the same way, when no insertions happen after the first query, sorting once and scanning in order can replace a priority queue (Skiena, n.d.c). A request for the item at a given rank, such as the median, is a selection problem (Skiena, n.d.b; Cormen et al., 2022).
Setup cost belongs in the comparison when the program reuses a structure many times. Converting a sparse matrix to the CSR5 storage format can cost as little as a few matrix–vector multiplications, so its value depends on how many multiplications follow (Liu & Vinter, 2015). On a European road network, one preprocessing configuration gave faster route queries at twice the preprocessing time (Gottesbüren et al., 2019).
Why asymptotic complexity misleads
Algorithm engineering treats design, analysis, implementation and experiments as a cycle. Experimental results can change the design or lead to better guarantees for the inputs that occur (Sanders, 2009; Chimani & Klein, 2010). How running time scales depends on the structure of the input as well as its size, which makes scaling partly an empirical question (Johnson, 2001).
The memory hierarchy (the levels of cache between the processor and main memory) is a main reason why counting operations fails to rank algorithms. In one sorting comparison, a cache-tuned quicksort beat radix sort by 24% on the largest data set. It did so while executing more than three times as many instructions (LaMarca & Ladner, 1999).
Whether to avoid branches, the points where a program chooses between paths, depends on input size. For repeated searches in arrays small enough to fit in the fastest cache, branch-free versions of binary search sometimes ran twice as fast as branching versions (Khuong & Morin, 2015). On larger arrays, branching search stalled less, because the processor speculatively started later iterations before the keys arrived (Schulz et al., 2018).
Lower hardware counts do not guarantee less time. In one search study, branch-free binary searches had the fewest cache misses yet ran slowly on large arrays. Prefetching two keys per step caused more misses and made them faster (Schulz et al., 2018).
Vector instructions (SIMD, which apply one operation to several values at once) give gains that shrink or reverse with conditions. On an Intel Haswell processor, a wider SIMD mergesort was 42% faster than a narrower one with one thread, but only 14% faster with eight threads (Inoue, 2016). In a web server test, encryption using wider vector instructions raised throughput for static files but lowered it when the server also compressed files on the fly (Gottschlag & Bellosa, 2019).
Cache-oblivious and cache-conscious designs
Studies disagree on whether cache-oblivious algorithms, which are designed to use any cache well without knowing its size, beat code tuned for a specific cache. A tuned cache-oblivious sort, Lazy Funnelsort, could beat the best tested quicksort on inputs that fit in memory, but lost to a careful multiway mergesort on disk (Brodal et al., 2007). In dense linear algebra, highly optimized cache-oblivious programs were significantly slower than cache-conscious ones (Yotov et al., 2007).
Crossovers where simple methods win Strong evidence: many studies agree on this
Below some input size a method with a worse bound often wins, and the crossover point depends on the machine and the workload. In one experiment on a Xeon processor, binary search was slightly faster than linear scans up to 32 values and slower at 64 (Khuong, 2012). For arrays of six to 16 items, sorting networks beat insertion sort by at least 1.76 times. The gain was smaller once they served as the small-case sorter inside quicksort (Bingmann et al., 2020).
Asymptotically faster methods carry overheads that set a minimum useful size. On an NVIDIA V100 GPU, one level of Strassen’s matrix multiplication first beat NVIDIA’s cuBLAS library at size 1,536, by at most 1.11 times. A two-level version with extra workspace crossed over only at 7,680 (Huang et al., 2018b). A parallel in-place samplesort run on one thread was slower than BlockQuicksort on uniform inputs of at most 2¹⁵ items because of its initial overhead (Axtmann et al., 2017).
Memory layout shifts the crossover as well. Across machines, plain sorted order was the better layout for binary search on small arrays. On larger ones the Eytzinger layout usually won; it stores the implicit search tree level by level (Khuong & Morin, 2015). Cutoffs do not always travel between processors: explicit prefetching made array search much slower on some tested processors, particularly at small sizes (Khuong & Morin, 2015).
Predicting performance without timing
Analytic performance models, which predict speed from the hardware instead of timing the code, fit some computations closely and others poorly. The Execution-Cache-Memory model, which adds up in-core work and data transfers between cache levels, fit measured performance within 10% for one stencil computation across three memory-bound phases (Stengel et al., 2015). The simpler Roofline model could not tell apart the blocking strategies in the same example, and the authors report the Execution-Cache-Memory model to be overoptimistic for some tight loops (Stengel et al., 2015).
Extrapolating from small test cases is also risky. Small inputs can predict large-input running times poorly when cache behaviour changes with size (Landrum et al., 1998).
Standard-library defaults
Guarantees and implementations differ between libraries and releases. The JavaScript specification requires a stable sort, one that keeps equal keys in their original order, but leaves the algorithm to each engine (V8, 2019). The table lists what each library documents for its main sort or map.
| Library | Operation | Documented implementation |
|---|---|---|
| Rust standard library | stable and unstable slice sort | driftsort and ipnsort (The Rust Project Developers, n.d.c) |
| Java (Oracle JDK 25) | Arrays.sort(int[]) | dual-pivot quicksort (Oracle, n.d.b) |
| CPython 3.14.0 | list sort | adaptive merge sort with Powersort choosing which runs to merge (Python Software Foundation, 2025b) |
| V8 (2019) | Array.prototype.sort | Timsort (V8, 2019) |
| libc++ (LLVM 20.1.0) | std::sort | introsort with insertion sort for small inputs, among other techniques (LLVM Project, n.d.c) |
| Go 1.24 | built-in map | Swiss Tables, a hash table design (Pratt, 2025) |
Specialized algorithms can beat these defaults by large factors in narrow conditions. GCC’s library introsort was at least 1.86 times slower than a sequential in-place samplesort on 2³² uniformly distributed items (Axtmann et al., 2017). A vectorized quicksort was three to eight times faster than the standard library sort on one million random numbers on a single Apple M1 Max core (Blacher et al., 2022). Its integration into a parallel sorter gave a geometric mean speedup (an average suited to ratios) of 1.59 (Wassenberg et al., 2022).
Large gains in isolated operations shrink in whole programs. Go’s maintainers measured map operations up to 60% faster in Go 1.24 than in 1.23, with some regressions, but about a 1.5% improvement in CPU time across full application benchmarks (Pratt, 2025). V8’s switch to Timsort was up to 17 times faster on a constructed input of two reverse-sorted runs, yet left nearly all workloads of a web tooling benchmark about unchanged (Zünd, 2018).
Benchmarking that can be trusted
A benchmark is trustworthy when it reports enough about the algorithms, inputs, computing environment and results for readers to attempt a comparable experiment (Johnson, 2001). Many published comparisons fall short: in selected 2011 systems venues, 71 of the 90 papers that reported running times gave no measure of variation (Kalibera & Jones, 2013).
Measurements vary at several levels, such as between builds, between process runs and between iterations within a run. Kalibera and Jones recommend putting the repetitions where the uncertainty is greatest (Kalibera & Jones, 2013). Running each experiment in many randomized setups reduces bias from the conditions of one particular setup (Mytkowicz et al., 2009).
Warm-up cannot be assumed to end after a fixed number of iterations in runtimes that compile code while it runs. Depending on machine and operating system, only 30.0–43.5% of the virtual machine and benchmark pairs studied reached a steady state of peak performance (Barrett et al., 2017). Performance metrics covers the wider question of what to measure.
Speed of code written by language models
Code generated by language models can be slower than human solutions, and the evidence on how much of the gap comes from algorithm choice is thin. In the EffiBench benchmark, GPT-4 solutions that passed the tests took on average 3.12 times as long as the human reference solutions; failing outputs were not timed (Huang et al., 2024b). Timeouts in another benchmark, PROBE, included brute-force enumeration and repeating expensive operations after each update (Nogueira et al., 2026b).
On real repositories, agents asked to speed up code fall short of experts on average. In the SWE-fficiency benchmark, the top tested agents averaged less than 0.23 times the expert speedup (Ma et al., 2026f). The gap is not only a matter of choosing the wrong approach. On 66 tasks where no public patch reached reference speed, 32 best patches used the human reference’s broad optimization. That agreement alone did not explain the gap (Chen et al., 2026d).
Audits question how reliably these benchmarks measure speed. Re-run on other machines, the human reference patches passed the original validity rules every time on only 39 of 102 GSO tasks. The rate was 11 of 140 on SWE-Perf and 411 of 498 on SWE-fficiency (Chen et al., 2026d). Two benchmarks ranked the same eight submissions differently on nine of 28 pairs (Chen et al., 2026d). On tasks where the benchmark showed no significant timing difference, tests generated by two language models found significant improvements in 24.01% and 25.43% of them (Le et al., 2026).
The best of many attempts does much better than the average attempt. At least one of ten public submissions matched or beat the human reference on 384 of 450 tasks that replayed validly (Chen et al., 2026d).
In one study, feeding profiler measurements back to the agent after each patch improved its results. With GPT-5.1, it matched expert speed on 39.2% of GSO tasks against 19.6% for a standard agent, and on 74% against 26% on a smaller SWE-fficiency set (Deng et al., 2026). Its patches also passed GSO’s correctness tests more often, 96.1% against 88.2% (Deng et al., 2026). See AI coding agents for the wider evidence on such agents.
Further reading
- Johnson (2001): a guide to designing, running and reporting experiments on algorithms, written for theorists.
- Chimani and Klein (2010): an introduction to algorithm engineering and its cycle of design, analysis, implementation and experiments.
- LaMarca and Ladner (1999): a sorting study showing that cache behaviour can reverse a ranking based on instruction counts.
- Kalibera and Jones (2013): how to plan repetitions so that benchmark results carry an honest measure of uncertainty.
- Skiena (1999): a catalogue that maps problems, by input and desired result, to algorithms and implementations.
- Chen et al. (2026d): an audit of the benchmarks used to measure how well coding agents optimize code.
First published .
Known gaps
What this article cannot tell you yet.
- Automatic per-input algorithm selection and autotuning, and how much they gain over a fixed choice, were not researched.
- Most hardware measurements predate current multicore processors and current runtime versions; newer measurements of the same effects were not gathered.
- Independent tests of the standard-library implementations described here (Go maps, Rust, Java, CPython, V8 and libc++ sorts) beyond their maintainers’ own reports were not researched.
- Whether the coding-agent results transfer to other repositories and workload distributions was not researched, and the profiler-guided agent workflow rests on one preprint.
- The SWE-fficiency benchmark paper was available only as an abstract.
- Which hardware effect (memory, branches or vector instructions) matters most across problem families was not researched.
References
- Solid source
- Rated highly trustworthy by the research, and every passage taken from it was found in it.
- Weak source
- Rated low, or a passage taken from it was not found in it.
- Medium trust
- Rated medium by the research.
- Not checked
- Rated high, but its passages could not all be checked against it.
Show all 42 references
- Axtmann, M., Witt, S., Ferizovic, D., & Sanders, P. (2017). In-place parallel super scalar samplesort (IPS4o). 25th Annual European Symposium on Algorithms (ESA 2017), Leibniz International Proceedings in Informatics. https://drops.dagstuhl.de/storage/00lipics/lipics-vol087-esa2017/LIPIcs.ESA.2017.9/LIPIcs.ESA.2017.9.pdf Solid source Peer-reviewed, read in large part.
- Barrett, E., Bolz-Tereick, C. F., Killick, R., Mount, S., & Tratt, L. (2017). Virtual machine warmup blows hot and cold. OOPSLA 2017. https://soft-dev.org/pubs/html/barrett_bolz-tereick_killick_mount_tratt__virtual_machine_warmup_blows_hot_and_cold_v6/ Medium trust Peer-reviewed, read in full.
- Bingmann, T., Marianczuk, J., & Sanders, P. (2020). Engineering faster sorters for small sets of items. Software: Practice and Experience. https://publikationen.bibliothek.kit.edu/1000127671/100508962 Solid source Peer-reviewed, read in large part.
- Blacher, M., Giesen, J., Sanders, P., & Wassenberg, J. (2022). Vectorized and performance-portable quicksort. arXiv. https://arxiv.org/html/2205.05982 Medium trust Peer-reviewed, read in large part.
- Brodal, G. S., Fagerberg, R., & Vinther, K. (2007). Engineering a cache-oblivious sorting algorithm. Journal of Experimental Algorithmics. https://cs.au.dk/~gerth/papers/jea07.pdf Solid source Peer-reviewed, read in large part.
- Chen, Z., Sun, Z., Shi, Y., Lo, D., & Jiang, L. (2026d). Are performance-optimization benchmarks reliably measuring coding agents? arXiv preprint. https://arxiv.org/html/2607.01211v1 Solid source Preprint, read in large part.
- Chimani, M., & Klein, K. (2010). Algorithm engineering: Concepts and practice. Experimental Methods for the Analysis of Optimization Algorithms, Springer. https://tcs.uos.de/_media/pubs/ae_chapter_preprint.pdf Solid source Preprint, read in large part.
- Cormen, T. H., Leiserson, C. E., Rivest, R. L., & Stein, C. (2022). Introduction to algorithms, fourth edition. The MIT Press. https://mitp-content-server.mit.edu/books/content/sectbyfn/books_pres_0/11599/4e_toc.pdf Solid source Book, read in large part.
- Deng, R., Liu, Y., Lipka, B., Ma, Y., Chen, X., Kaler, T., et al. (2026). PerfAgent: Profiler-guided iterative refinement for repository-level code optimization. arXiv preprint. https://arxiv.org/html/2607.19653v1 Medium trust Preprint, read in large part.
- Gottesbüren, L., Hamann, M., Uhl, T. N., & Wagner, D. (2019). Faster and better nested dissection orders for customizable contraction hierarchies. Algorithms. https://www.mdpi.com/1999-4893/12/9/196 Medium trust Peer-reviewed, read in large part.
- Gottschlag, M., & Bellosa, F. (2019). Reducing AVX-induced frequency variation with core specialization. 9th Workshop on Systems for Multi-core and Heterogeneous Architectures. https://os.itec.kit.edu/downloads/2019_mathias_gottschlag_sfma_paper.pdf Solid source Peer-reviewed, read in large part.
- Huang, J., Yu, C. D., & van de Geijn, R. A. (2018b). Implementing Strassen's algorithm with CUTLASS on NVIDIA Volta GPUs. arXiv preprint. https://arxiv.org/abs/1808.07984 Medium trust Preprint, read in large part.
- Huang, D., Qing, Y., Shang, W., Cui, H., & Zhang, J. M. (2024b). EffiBench: Benchmarking the efficiency of automatically generated code. NeurIPS 2024, Datasets and Benchmarks Track. https://papers.neurips.cc/paper_files/paper/2024/file/15807b6e09d691fe5e96cdecde6d7b80-Paper-Datasets_and_Benchmarks_Track.pdf Solid source Peer-reviewed, read in large part.
- Inoue, H. (2016). How SIMD width affects energy efficiency: A case study on sorting. COOL Chips. https://s3.us.cloud-object-storage.appdomain.cloud/res-files/1580-COOLChips_SIMDsort.pdf Medium trust Peer-reviewed, read in large part.
- Johnson, D. S. (2001). A theoretician's guide to the experimental analysis of algorithms. AT&T Labs Research. https://usermanual.wiki/Pdf/atheoreticiansguidetotheexperimentalanalysisofalgorithms.929924641.pdf Solid source Preprint, read in large part.
- Kalibera, T., & Jones, R. (2013). Rigorous benchmarking in reasonable time. ACM SIGPLAN International Symposium on Memory Management (ISMM 2013). https://kar.kent.ac.uk/33611/45/p63-kaliber.pdf Solid source Peer-reviewed, read in large part.
- Khuong, P. (2012). Binary search eliminates branch mispredictions. Paul Khuong: some Lisp (blog). https://pvk.ca/Blog/2012/07/03/binary-search-star-eliminates-star-branch-mispredictions/ Solid source Blog post, read in large part.
- Khuong, P., & Morin, P. (2015). Array layouts for comparison-based searching. Carleton University. https://cglab.ca/~morin/misc/arraylayout-v2/arraylayouts.pdf Solid source Preprint, read in large part.
- LaMarca, A., & Ladner, R. E. (1999). The influence of caches on the performance of sorting. Journal of Algorithms. https://www.cs.auckland.ac.nz/~mcw/Teaching/refs/sorting/ladner-lamarca-cach-sorting.pdf Solid source Peer-reviewed, read in large part.
- Landrum, J., Hardwick, J., & Stout, Q. F. (1998). Predicting algorithm performance. Computing Science and Statistics. https://web.eecs.umich.edu/~qstout/pap/IF98pred.pdf Medium trust Peer-reviewed, read in large part.
- Le, N. M., Xu, Y., Wang, Z., & Chen, T. (2026). Rethinking code performance benchmarks for LLMs. arXiv preprint. https://arxiv.org/html/2607.07619v1 Solid source Preprint, read in large part.
- Liu, W., & Vinter, B. (2015). CSR5: An efficient storage format for cross-platform sparse matrix-vector multiplication. Proceedings of the 29th ACM International Conference on Supercomputing. https://www.ssslab.cn/assets/papers/2015-liu-csr5.pdf Solid source Peer-reviewed, read in large part.
- LLVM Project (n.d.c). sort.h. libc++ in LLVM 20.1.0. https://raw.githubusercontent.com/llvm/llvm-project/llvmorg-20.1.0/libcxx/include/__algorithm/sort.h Solid source Read in full.
- Ma, J. J., Hashemi, M., Yazdanbakhsh, A., Swersky, K., Press, O., Li, E., et al. (2026f). SWE-fficiency: Can language models optimize real-world repositories on real workloads? Proceedings of the 43rd International Conference on Machine Learning. https://proceedings.mlr.press/v306/ma26l.html Solid source Peer-reviewed, abstract only.
- Mytkowicz, T., Diwan, A., Hauswirth, M., & Sweeney, P. F. (2009). Producing wrong data without doing anything obviously wrong! ACM SIGPLAN Notices. https://john.cs.olemiss.edu/~hcc/researchMethods/notes/localcopy/mytkowicz-wrong-data.pdf Solid source Peer-reviewed, read in large part.
- Nogueira, R. P., Vieira, M., & Campos, J. R. (2026b). PROBE: Benchmarking code generation in large language models. Empirical Software Engineering. https://link.springer.com/article/10.1007/s10664-026-10904-5 Solid source Peer-reviewed, read in large part.
- Oracle (n.d.b). Arrays (Java SE 25 and JDK 25). Java SE 25 API documentation, Oracle. https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/Arrays.html Solid source Documentation, read in large part.
- Pratt, M. (2025). Faster Go maps with Swiss Tables. The Go Blog. https://go.dev/blog/swisstable Medium trust Vendor page, read in large part.
- Python Software Foundation (2025b). listsort.txt. CPython v3.14.0 source. https://raw.githubusercontent.com/python/cpython/v3.14.0/Objects/listsort.txt Medium trust Documentation, read in full.
- The Rust Project Developers (n.d.c). slice (primitive type), Rust standard library. Rust documentation. https://doc.rust-lang.org/std/primitive.slice.html Solid source Documentation, read in large part.
- Sanders, P. (2009). Algorithm engineering: An attempt at a definition. Efficient Algorithms, Lecture Notes in Computer Science 5760, Springer. https://link.springer.com/chapter/10.1007/978-3-642-03456-5_22 Solid source Book, abstract only.
- Schulz, L., Broneske, D., & Saake, G. (2018). An eight-dimensional systematic evaluation of optimized search algorithms on modern processors. Proceedings of the VLDB Endowment, 11. https://www.vldb.org/pvldb/vol11/p1550-schulz.pdf Solid source Peer-reviewed, read in large part.
- Skiena, S. (1999). The Stony Brook Algorithms Repository. Stony Brook University. https://www3.cs.stonybrook.edu/~skiena/hits/node2.html Solid source Read in full.
- Skiena, S. (n.d.a). Dictionaries. Stony Brook Algorithm Repository. https://algorist.com/problems/Dictionaries.html Medium trust Read in full.
- Skiena, S. (n.d.c). Priority queues. Stony Brook Algorithm Repository. https://algorist.com/problems/Priority_Queues.html Medium trust Read in full.
- Skiena, S. (n.d.d). Searching. Stony Brook Algorithm Repository. https://algorist.com/problems/Searching.html Medium trust Read in full.
- Skiena, S. (n.d.b). Median and selection. Stony Brook Algorithm Repository. https://algorist.com/problems/Median_and_Selection.html Medium trust Read in full.
- Stengel, H., Treibig, J., Hager, G., & Wellein, G. (2015). Quantifying performance bottlenecks of stencil computations using the Execution-Cache-Memory model. arXiv. https://arxiv.org/html/1410.5010 Solid source Peer-reviewed, read in large part.
- V8 (2019). Stable Array.prototype.sort. V8. https://v8.dev/features/stable-sort Solid source Documentation, read in large part.
- Wassenberg, J., Blacher, M., Giesen, J., & Sanders, P. (2022). Vectorized and performance-portable quicksort. Software: Practice and Experience. https://onlinelibrary.wiley.com/doi/abs/10.1002/spe.3142 Solid source Peer-reviewed, abstract only.
- Yotov, K., Roeder, T., Pingali, K., Gunnels, J., & Gustavson, F. (2007). An experimental comparison of cache-oblivious and cache-conscious programs. Proceedings of the Nineteenth Annual ACM Symposium on Parallel Algorithms and Architectures. https://www.cs.utexas.edu/~pingali/CS378/2008sp/papers/co.pdf Solid source Peer-reviewed, read in large part.
- Zünd, S. (2018). Getting things sorted in V8. V8. https://v8.dev/blog/array-sort Medium trust Blog post, read in large part.