DEV Community

Harry Phung
Harry Phung

Posted on Originally published at blog.quangpv.top on

Big O: Don't talk about speed!

Big O: Don't talk about speed!

In the world of computer science, we are often taught a simple hierarchy: (O(1) < O(log N) < O(N) < O(N log N) < O(N^2)). We are led to believe that an (O(N)) algorithm is inherently "better" than an (O(N^2)) one.

However, in the real world, this is a dangerous oversimplification. Big O notation is a tool for classifying growth rates as (N) approaches infinity, not a stopwatch for measuring absolute execution time for practical data sizes. One of the most common pitfalls in performance optimization is ignoring the "hidden constants" and the reality of modern hardware.

Big O: Don't talk about speed!

1. The Deception of Hidden Constants

Let’s look at a concrete example. Suppose you are choosing between two algorithms to solve the same problem:

  • Algorithm A : Complexity is (O(100,000,000 times N)) -> In Big O terms, we drop the constant, so it is (O(N)).
  • Algorithm B : Complexity is (O(2 times N^2)) -> Dropping the constant, it is (O(N^2)).

On paper, a junior developer would choose Algorithm A without hesitation. But let’s see what happens when we run these algorithms with (N = 100):

  • Algorithm B takes: (2 times 100^2 = 20,000) steps.
  • Algorithm A takes: (100,000,000 times 100 = 10,000,000,000) (10 billion) steps!

With (N = 100), Algorithm B is 500,000 times faster than Algorithm A. While A is theoretically "better," it is practically unusable for small inputs due to its massive constant factor.

2. The Crossover Point: Where Theory Meets Reality

So, when does (O(N)) actually become faster? Only when (N) is large enough.

This is known as the "Crossover Point." On a performance graph, the (O(N^2)) curve starts lower but grows quadratically. The (O(N)) curve starts much higher due to the constant but grows linearly. They eventually intersect at the crossover point.

In our example, the crossover point is at (N = 50,000,000). Unless your dataset is larger than 50 million elements, the (O(N^2)) algorithm is actually the superior choice.

3. Hardware Reality: The Critical Cache Factor

Big O assumes all operations are equal. In reality, hardware costs vary wildly. A single addition is fast, but a memory access can be a bottleneck. Specifically, reading from Main Memory (RAM) can be 100x slower than reading from the CPU Cache (L1/L2).

This leads to a fascinating reality: An (O(N)) algorithm that jumps around randomly in memory (causing constant Cache Misses) can be significantly slower than an (O(N^2)) algorithm that accesses data sequentially (utilizing Cache Locality). The theoretical "fewer operations" of (O(N)) are completely wiped out by the massive latency of the hardware.

4. Real-World Data & The Trap of Micro-Optimization

Real-world data size is the most important factor. Before you complicate your codebase with advanced data structures just to reduce a Big O class, ask yourself: "How large is my data really?"

If your array rarely exceeds 100 elements, switching from a simple nested loop to a complex balanced tree might actually slow down your program due to setup overhead and poor cache usage.

Prioritize benchmarking over theoretical complexity. Use profiling tools to measure actual execution time with real-world data. Avoid "micro-performance" improvements that make the code unreadable without providing a measurable gain.

5. Final Conclusion: Developing a Pragmatic Perspective

To summarize the key takeaways:

  • Big O is a classification tool, not a measurement : It describes how an algorithm scales, not how fast it runs on your machine today.
  • Don't ignore constants and hardware : Large constants or poor cache locality can make a "better" Big O class perform worse in practice.
  • Real-world data is king : Always measure and benchmark with actual data sizes before deciding to optimize. Avoid premature micro-optimization that adds unnecessary complexity.

Understanding these nuances will give you a much sharper perspective on algorithm design. It provides you with solid arguments when debating with colleagues during design reviews and helps you stand out to interviewers by demonstrating a pragmatic, senior-level approach to performance engineering.

Top comments (0)