We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
To put some tangible numbers behind this, I implemented the two functions that are O(3n) and O(n), then also added an implementation that should be much slower (but is also very readable and faster to implement) that is O(nlogn). The results are suprising:
The theoretically fastest solution is also the slowest of the group. I can think of a few microarchitectural reasons why that might be the case including a pair of floating point comparisons, but TLDR: always benchmark and regression test code that actually has to run fast. Don't rely on trivial Big-O analysis.
Mini-Max Sum
You are viewing a single comment's thread. Return to all comments →
To put some tangible numbers behind this, I implemented the two functions that are O(3n) and O(n), then also added an implementation that should be much slower (but is also very readable and faster to implement) that is O(nlogn). The results are suprising:
O(nlogn + 2n): 1.2469678640000001 O(3n): 1.1900847209999998 O(n): 1.2989587239999998
The theoretically fastest solution is also the slowest of the group. I can think of a few microarchitectural reasons why that might be the case including a pair of floating point comparisons, but TLDR: always benchmark and regression test code that actually has to run fast. Don't rely on trivial Big-O analysis.