3 ms·
In my experience it is better to default to the lowest complexity algorithm. Even if linear search is faster for small N your app probably performs acceptably f
by kevincox 4y ago
In my experience it is better to default to the lowest complexity algorithm. Even if linear search is faster for small N your app probably performs acceptably for small N either way. At some point being fast enough to be interactive is good enough and there isn't much point making your interactions take 2ms rather than 10ms.
However n^2 algorithms will bite you. They may be outside of your everyday working set, and even dogfooding you may not have larger datasets than all of your users. At some point one user will hit a dimension that you "expected to always be small" and get bad performance.
If one of these O(1)N algorithms ends up in the hot path you can always optimize it when profiling points it out. But O(N)N algorithms will leap off the cold path and become hot when some user starts using the "wrong" data patterns and the developers may never be aware, or at least not aware until it is too late.