Default to Less Than Quadratic
Tags: performance
Published: 2023-05-11 18:53:37

---
title: Default to Less Than Quadratic
published: true
date: 2023-05-09 21:50:00 UTC
tags: Performance
canonical_url: https://kevincox.ca/2023/05/09/less-than-quadratic/
---

<main style="--width:min(36em,100%);display:grid;grid-template-columns:1fr var(--width) 1fr;overflow:auto;padding:0;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">When programming you often need to pick an algorithm for small tasks. For example if you need to select an account by name a bunch of algorithms probably spring to mind. For example linear search over the array or adding a hashmap to lookup by name in constant time. Which one should you choose?</p>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">It is well studied that simpler algorithms can perform better on small datasets despite worse complexity. For example bubblesort is one of the fastest algorithms for sorting small arrays and linear search can search through hundreds of integers in the time it takes to even compute a moderately complex hash.</p>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">So what algorithm do you choose? It can be tempting to pick one of these simpler algorithms if you know your data will typically be small. Most users are probably signed into a single account and few if any people will have over ten. So optimizing for a small number of accounts will be faster for almost, if not all, users and is likely simpler to implement.</p>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">My default advice is to <strong style="box-sizing:border-box;margin:0">choose the simplest algorithm with less than quadratic time and space complexity</strong>. Every situation is different, but unless I have a good reason for an exception I stick to this rule.</p>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">The reasons for this are simple:</p>
<ul style='box-sizing:border-box;grid-column:2;list-style:disc;list-style:"- ";margin:0 1rem .8em'>
<li style="box-sizing:border-box;margin:0">Most code isn’t performance sensitive. Don’t spend too much time speculating about the performance, just make it decent by default. Slow apps are generally slow due to specific performance or architecture issues, not due to lack of attention to the tiny details in every line of code.</li>
<li style="box-sizing:border-box;margin:0">Linear performance scaling is understandable and only moderately painful. Users won’t like seeing slow software, but they understand how having twice as much data can make it twice as slow. The performance also degrades incrementally, which helps it be tolerable while you are working on a fix.</li>
<li style="box-sizing:border-box;margin:0">For most use cases it is more important to have decent performance in all cases than great performance in the simple cases. If your software is generally performant then the simple cases are already acceptably fast, there is little user benefit to optimizing them further. On the other hand, complex cases will be pushing the limit of your users’ patience already, it is good to avoid slowing down further.</li>
<li style="box-sizing:border-box;margin:0">Slow performance in normal use cases is likely to be noticed by developers and fixed quickly. Profiling will help target optimization work to the hotspots. Extreme cases are less likely to be hit by developers and most users, so may linger for longer hurting the minority.</li>
</ul>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">I really like this quote from <a href="https://twitter.com/BruceDawson0xB/status/1120381406700429312" style="box-sizing:border-box;margin:0;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray">Bruce Dawson</a>:</p>
<blockquote style="box-sizing:border-box;font-size:1.2em;grid-column:2;margin:1em;padding:.6em;position:relative"><p style="box-sizing:border-box;font-style:italic;margin:0 1rem;padding-bottom:0;padding-top:0;text-align:center">O(n<sup style="box-sizing:border-box;font-style:italic;margin:0;text-align:center">2</sup>) is the sweet spot of badly scaling algorithms: fast enough to make it into production, but slow enough to make things fall down once it gets there</p></blockquote>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">If you are frequently putting quadratic algorithms into your code your users are going to see it fall down when they pushed it in a dimension that you expected to “always be small”.</p>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">It is also important to remember this observation from the <a href="https://accidentallyquadratic.tumblr.com/post/113840433022/why-accidentally-quadratic" style="box-sizing:border-box;margin:0;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray">Accidentally Quadratic</a> blog.</p>
<blockquote style="box-sizing:border-box;font-size:1.2em;grid-column:2;margin:1em;padding:.6em;position:relative"><p style="box-sizing:border-box;font-style:italic;margin:0 1rem;padding-bottom:0;padding-top:0;text-align:center">The property of linearity, however, is not closed under composition. If you do a linear operation linearly many times, you get quadratic!</p></blockquote>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">Even if you always pick sub-quadratic algorithms the composition may not be. So you should consider the complexity of the system as a whole. Linear search to find an account in a list is fast enough for my rule, but if you are doing this for each account then you have an O(n<sup style="box-sizing:border-box;margin:0">2</sup>) system overall, and I would recommend optimizing it.</p>
<h2 id="example" style="box-sizing:border-box;grid-column:2;margin:1.5em 1rem .8em"><a href="https://kevincox.ca/2023/05/09/less-than-quadratic/#example" style="box-sizing:border-box;color:inherit!important;margin:0;text-decoration:inherit;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray;text-shadow:inherit!important">Example</a></h2>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">As a concrete example. To make a list of unique elements two functions came to mind. The first is an O(n<sup style="box-sizing:border-box;margin:0">2</sup>) algorithm that does the simple thing of checking if each element is in the output list before adding it.</p>
<div style="background:#222;border-bottom:1px solid #444;border-top:1px solid #444;box-sizing:border-box;display:grid;grid-column:2;grid-column:1/4;grid-template-columns:1fr var(--width) 1fr;margin:0 0 .8em;overflow-x:auto;padding:0"><pre style="box-sizing:border-box;grid-column:2;margin:.4em 1rem"><code style="background:none!important;box-sizing:border-box;color:#fff;margin:0;white-space:pre-wrap;white-space:pre"><span style="box-sizing:border-box;color:#8585e0;font-weight:700;margin:0;padding:0">fn</span> <span style="box-sizing:border-box;margin:0;padding:0">unique</span><span style="box-sizing:border-box;color:#f5a3a3;font-weight:700;margin:0;padding:0">&lt;</span><span style="box-sizing:border-box;color:#759ef0;margin:0;padding:0">'a</span><span style="box-sizing:border-box;color:#f5a3a3;font-weight:700;margin:0;padding:0">&gt;</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">(</span><span style="box-sizing:border-box;margin:0;padding:0">names</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">:</span> <span style="box-sizing:border-box;color:#f5a3a3;font-weight:700;margin:0;padding:0">&amp;</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">[</span><span style="box-sizing:border-box;color:#f5a3a3;font-weight:700;margin:0;padding:0">&amp;</span><span style="box-sizing:border-box;color:#759ef0;margin:0;padding:0">'a</span> <span style="box-sizing:border-box;margin:0;padding:0">str</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">])</span> <span style="box-sizing:border-box;color:#8585e0;font-weight:700;margin:0;padding:0">-&gt;</span> <span style="box-sizing:border-box;margin:0;padding:0">Vec</span><span style="box-sizing:border-box;color:#f5a3a3;font-weight:700;margin:0;padding:0">&lt;&amp;</span><span style="box-sizing:border-box;color:#759ef0;margin:0;padding:0">'a</span> <span style="box-sizing:border-box;margin:0;padding:0">str</span><span style="box-sizing:border-box;color:#f5a3a3;font-weight:700;margin:0;padding:0">&gt;</span> <span style="box-sizing:border-box;color:#999;margin:0;padding:0">{</span>
	<span style="box-sizing:border-box;color:#8585e0;font-weight:700;margin:0;padding:0">let</span> <span style="box-sizing:border-box;color:#8585e0;font-weight:700;margin:0;padding:0">mut</span> <span style="box-sizing:border-box;margin:0;padding:0">result</span> <span style="box-sizing:border-box;color:#f5a3a3;font-weight:700;margin:0;padding:0">=</span> <span style="box-sizing:border-box;margin:0;padding:0">Vec</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">::</span><span style="box-sizing:border-box;margin:0;padding:0">new</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">();</span>
	<span style="box-sizing:border-box;color:#8585e0;font-weight:700;margin:0;padding:0">for</span> <span style="box-sizing:border-box;margin:0;padding:0">name</span> <span style="box-sizing:border-box;color:#8585e0;font-weight:700;margin:0;padding:0">in</span> <span style="box-sizing:border-box;margin:0;padding:0">names</span> <span style="box-sizing:border-box;color:#999;margin:0;padding:0">{</span>
		<span style="box-sizing:border-box;color:#8585e0;font-weight:700;margin:0;padding:0">if</span> <span style="box-sizing:border-box;color:#f5a3a3;font-weight:700;margin:0;padding:0">!</span><span style="box-sizing:border-box;margin:0;padding:0">result</span><span style="box-sizing:border-box;margin:0;padding:0">.contains</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">(</span><span style="box-sizing:border-box;margin:0;padding:0">name</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">)</span> <span style="box-sizing:border-box;color:#999;margin:0;padding:0">{</span>
			<span style="box-sizing:border-box;margin:0;padding:0">result</span><span style="box-sizing:border-box;margin:0;padding:0">.push</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">(</span><span style="box-sizing:border-box;color:#f5a3a3;font-weight:700;margin:0;padding:0">*</span><span style="box-sizing:border-box;margin:0;padding:0">name</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">);</span>
		<span style="box-sizing:border-box;color:#999;margin:0;padding:0">}</span>
	<span style="box-sizing:border-box;color:#999;margin:0;padding:0">}</span>
	<span style="box-sizing:border-box;color:#8585e0;font-weight:700;margin:0;padding:0">return</span> <span style="box-sizing:border-box;margin:0;padding:0">result</span>
<span style="box-sizing:border-box;color:#999;margin:0;padding:0">}</span>
</code></pre></div>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">However, I would advise against this function unless you know for sure that the inputs will <strong style="box-sizing:border-box;margin:0">never</strong> be large. I would instead prefer to write this O(n) algorithm.</p>
<div style="background:#222;border-bottom:1px solid #444;border-top:1px solid #444;box-sizing:border-box;display:grid;grid-column:2;grid-column:1/4;grid-template-columns:1fr var(--width) 1fr;margin:0 0 .8em;overflow-x:auto;padding:0"><pre style="box-sizing:border-box;grid-column:2;margin:.4em 1rem"><code style="background:none!important;box-sizing:border-box;color:#fff;margin:0;white-space:pre-wrap;white-space:pre"><span style="box-sizing:border-box;color:#8585e0;font-weight:700;margin:0;padding:0">pub</span> <span style="box-sizing:border-box;color:#8585e0;font-weight:700;margin:0;padding:0">fn</span> <span style="box-sizing:border-box;margin:0;padding:0">unique</span><span style="box-sizing:border-box;color:#f5a3a3;font-weight:700;margin:0;padding:0">&lt;</span><span style="box-sizing:border-box;color:#759ef0;margin:0;padding:0">'a</span><span style="box-sizing:border-box;color:#f5a3a3;font-weight:700;margin:0;padding:0">&gt;</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">(</span><span style="box-sizing:border-box;margin:0;padding:0">names</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">:</span> <span style="box-sizing:border-box;color:#f5a3a3;font-weight:700;margin:0;padding:0">&amp;</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">[</span><span style="box-sizing:border-box;color:#f5a3a3;font-weight:700;margin:0;padding:0">&amp;</span><span style="box-sizing:border-box;color:#759ef0;margin:0;padding:0">'a</span> <span style="box-sizing:border-box;margin:0;padding:0">str</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">])</span> <span style="box-sizing:border-box;color:#8585e0;font-weight:700;margin:0;padding:0">-&gt;</span> <span style="box-sizing:border-box;margin:0;padding:0">Vec</span><span style="box-sizing:border-box;color:#f5a3a3;font-weight:700;margin:0;padding:0">&lt;&amp;</span><span style="box-sizing:border-box;color:#759ef0;margin:0;padding:0">'a</span> <span style="box-sizing:border-box;margin:0;padding:0">str</span><span style="box-sizing:border-box;color:#f5a3a3;font-weight:700;margin:0;padding:0">&gt;</span> <span style="box-sizing:border-box;color:#999;margin:0;padding:0">{</span>
	<span style="box-sizing:border-box;color:#8585e0;font-weight:700;margin:0;padding:0">let</span> <span style="box-sizing:border-box;color:#8585e0;font-weight:700;margin:0;padding:0">mut</span> <span style="box-sizing:border-box;margin:0;padding:0">result</span> <span style="box-sizing:border-box;color:#f5a3a3;font-weight:700;margin:0;padding:0">=</span> <span style="box-sizing:border-box;margin:0;padding:0">Vec</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">::</span><span style="box-sizing:border-box;margin:0;padding:0">new</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">();</span>
	<span style="box-sizing:border-box;color:#8585e0;font-weight:700;margin:0;padding:0">let</span> <span style="box-sizing:border-box;color:#8585e0;font-weight:700;margin:0;padding:0">mut</span> <span style="box-sizing:border-box;margin:0;padding:0">seen</span> <span style="box-sizing:border-box;color:#f5a3a3;font-weight:700;margin:0;padding:0">=</span> <span style="box-sizing:border-box;margin:0;padding:0">std</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">::</span><span style="box-sizing:border-box;margin:0;padding:0">collections</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">::</span><span style="box-sizing:border-box;margin:0;padding:0">HashSet</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">::</span><span style="box-sizing:border-box;margin:0;padding:0">new</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">();</span>
	<span style="box-sizing:border-box;color:#8585e0;font-weight:700;margin:0;padding:0">for</span> <span style="box-sizing:border-box;margin:0;padding:0">name</span> <span style="box-sizing:border-box;color:#8585e0;font-weight:700;margin:0;padding:0">in</span> <span style="box-sizing:border-box;margin:0;padding:0">names</span> <span style="box-sizing:border-box;color:#999;margin:0;padding:0">{</span>
		<span style="box-sizing:border-box;color:#8585e0;font-weight:700;margin:0;padding:0">if</span> <span style="box-sizing:border-box;margin:0;padding:0">seen</span><span style="box-sizing:border-box;margin:0;padding:0">.insert</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">(</span><span style="box-sizing:border-box;margin:0;padding:0">name</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">)</span> <span style="box-sizing:border-box;color:#999;margin:0;padding:0">{</span>
			<span style="box-sizing:border-box;margin:0;padding:0">result</span><span style="box-sizing:border-box;margin:0;padding:0">.push</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">(</span><span style="box-sizing:border-box;color:#f5a3a3;font-weight:700;margin:0;padding:0">*</span><span style="box-sizing:border-box;margin:0;padding:0">name</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">);</span>
		<span style="box-sizing:border-box;color:#999;margin:0;padding:0">}</span>
	<span style="box-sizing:border-box;color:#999;margin:0;padding:0">}</span>
	<span style="box-sizing:border-box;color:#8585e0;font-weight:700;margin:0;padding:0">return</span> <span style="box-sizing:border-box;margin:0;padding:0">result</span>
<span style="box-sizing:border-box;color:#999;margin:0;padding:0">}</span>
</code></pre></div>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">This is slightly more complex, but does that hurt us for the small datasets that we expect? Yes, but it probably doesn’t matter. The algorithms cross at about 60 elements, at that point the runtime is less than 2µs. At worst the linear version is going to cost you about 600ns at 30 elements. It just doesn’t matter in 99.9% of cases.</p>
<p style="box-sizing:border-box;grid-column:2;margin:0 1rem .8em">If this function <strong style="box-sizing:border-box;margin:0">does</strong> ever show up on your profiles it is easy to add a length check to select the optimal algorithm for the data size. This way you can have the best of both worlds, great performance on small datasets but linear growth. However, I don’t think this complexity is worth it for most code. I default the simple sub-quadratic algorithm, then profile my code and spend my where it counts.</p></main>