algorithmic complexity vs hardware reality

Big-O lies: cache locality and branch prediction beat algorithmic complexity on real CPUs ACCU Conference
TL;DW
  • Algorithmic complexity alone doesn't predict real-world performance; modern CPUs depend on cache locality, branch prediction, and hardware constraints.
  • Vector operations outperform hash tables on small datasets (10K elements) due to better cache locality; hash table advantage emerges only at millions of elements.
  • Branch predictors use TAGE-SCL with four predictors inside modern CPUs, eliminating need for manual branch optimization in most cases.
  • Amortized complexity (like vector push_back) uses potential functions to hide reallocation costs; O(1) average doesn't mean O(1) every operation.
  • SIMD and parallel execution often regress performance unless workload exceeds millions of elements; single-threaded code typically optimizes better.
  • Cache misses cause performance valleys in multi-threaded code; adding threads beyond saturation point increases overhead faster than throughput.
  • Bloom filters and bitmaps enable cache-friendly set membership without allocating full hash tables; bitsets best if size known at compile time.
  • Bucketing large arrays into smaller chunks (3x faster than naive approaches) restores cache efficiency by keeping working set within L1/L2.
  • Division costs ~20x more cycles than addition on CPUs; multiplication 2x faster than division for floating-point; compilers auto-optimize integer shifts.
  • Modern C++26 SIMD eliminates manual CPU instruction writing via overloaded operators, but overhead significant unless data-parallel workload exceeds threshold.

Alex Dathskovsky at ACCU walks through concrete benchmarks where O(n²) cache-friendly code beats O(n) hash-table solutions, vectors outperform hash maps 3.6x on histograms, and single-threaded code beats parallel accumulation—then covers bitmap filtering, bucketing, and bit-packing as hardware-aware alternatives.