complexity

The performance chapter opens with a sentence worth keeping: The most important attribute of a program is correctness A correct program can still be useless if it wants more memory than exists or more time than a human will wait

I learned that with a resolver that walked an address space one address at a time It was correct It would have finished sometime next month Java would have made a wrapper class for the address and still been slow Assembly would have been fast and unmaintainable C++ let me choose the representation and then blame myself for the choice

Two ways to know

Analytical methods are separated from experimental measurement

Analytical means counting work on paper before code exists Experimental means running it with a clock after code exists

Paper first Clock second If you measure before you understand, you will optimise the wrong loop and feel very productive

Operation count

bool contains(const std::vector<int> &items, int target) {
    for (int item : items)
        if (item == target) return true;
    return false;
}

The comparison is the dominant operation With n items the worst case does n comparisons Best case does one Average depends on where the target tends to sit

An algorithm has more than one cost Best, worst, and average are different claims about the same code

Step count

Operation count is honest but noisy Step count groups operations into a formula such as 3n + 7

That explains why constants stop mattering at scale 10n + 7 and 3n are both linear n^2 eventually wins no matter how small its coefficient

The table in chapter three runs from constant through logarithmic, linear, n log n, quadratic, cubic, exponential, factorial That table is a map of pain

Big oh, honestly

Big oh is an upper bound When we write f(n) = O(g(n)), we say g grows at least as fast as f, up to a constant factor

12n + 6        -> O(n)
3n^2 + 2n + 6  -> O(n^2)
8n^4 + 9n^2    -> O(n^4)

Drop coefficients Keep the biggest unit term Do not write O(3n) when O(n) is the customary form That is like putting a version number on a lie

What notation hides

Big oh says nothing about constants in the real world An O(n) pass over a slow disk can lose to an O(n log n) pass in memory It says nothing about memory either A merge sort is predictable in time and allocates helper arrays An in place sort saves memory and may be slower

Say which resource you are measuring

The C++ cost model

std::vector gives contiguous memory and cheap iteration Inserting in the middle moves elements std::list makes insertion cheap and iteration cache hostile std::unordered_set gives average constant membership and occasional pathological behaviour

Those are not API footnotes They are the algorithm

Amortised cost

Amortised complexity is deliberately delayed until after queues A dynamic array doubles capacity when full That one push is expensive Most pushes are cheap Average over the sequence and the cost is constant

Judging one call gives the wrong impression Judging a benchmark with one call gives a beautiful lie

Worked comparison

Ten thousand hosts:

  • linear search over a vector , about ten thousand comparisons worst case
  • binary search over sorted data , about fourteen comparisons
  • unordered_set membership , about one comparison on average

Those are shapes, not promises They tell you which idea to reach for before a profiler tells you which line to fix

Mistakes

  • quoting big oh without saying worst or average
  • assuming a smaller big oh always wins at small input
  • ignoring allocation until the process dies
  • optimising a loop that is not on the hot path

Exercises

  1. Count the dominant operation in a nested loop comparing every pair
  2. Explain why 4n^2 + 10n is reported as O(n^2)
  3. Find a case where a linear scan beats a hash lookup in practice
  4. Measure two structures on the same input and explain the difference

What to remember

Correctness first Then count Then measure In that order, every time

Posted