Complexity

More about Big-O notation

Big-O Comparison

See why an algorithm's complexity class matters more than a fast computer. Switch nine classes from O(1) to O(n!) on and off, plot their step counts up to the n you choose on a linear or logarithmic axis, and read in the table how many steps each needs for your input size and how long that takes at a billion steps a second, from nanoseconds to longer than the age of the universe. Switch the complexity classes on or off with the chips above the chart, set how far the chart goes and whether its step axis is linear or logarithmic. Enter an input size n and a speed in steps per second to read each class's step count and running time in the table. Point at the chart to read the steps at any n; your settings are kept in this browser.

How it works

  1. Switch the complexity classes you want to compare on or off with the chips above the chart.
  2. Set how far the chart goes and choose a linear or a logarithmic step axis; point at the chart to read the steps at any n.
  3. Enter an input size n and a speed in steps per second, then read each class's step count and running time in the table and copy it.

What the table shows

For each class the table counts the steps at your n, with constant factors left out, and divides them by the speed. At n = 1,000 and a billion steps a second, O(n) takes 1 microsecond, O(n log n) 10 microseconds, O(n²) 1 millisecond and O(n³) 1 second, while O(2ⁿ) needs about 10²⁸⁴ years, far longer than the 13.8 billion years the universe has existed. Counts too big for a computer number are kept as logarithms and written as powers of ten.

Linear or logarithmic axis

On a linear axis the fast-growing classes leave the chart within a few steps of n, which shows how quickly they become hopeless: the height is set by the polynomial classes, and O(2ⁿ) and O(n!) shoot out of the top. A logarithmic axis gives each power of ten the same height, so every class fits: the polynomials become gently rising curves, O(2ⁿ) a straight line and O(n!) a line that keeps getting steeper.

Frequently asked questions

What does Big-O notation mean?

It describes how the running time or memory of an algorithm grows with the size n of its input, ignoring constant factors and smaller terms. An algorithm that takes 3n² + 5n + 2 steps is O(n²): double n and it takes about four times as long, whatever computer runs it.

Why are the logarithms base 2?

Because halving is what most O(log n) algorithms do: binary search halves the list at each step, so it needs about log₂ n comparisons. In Big-O the base does not matter, since log₂ n and log₁₀ n differ only by a constant factor of about 3.32.

Is an O(n log n) algorithm always faster than an O(n²) one?

Not for every n. Big-O hides the constant factors, so for small inputs a simple O(n²) method such as insertion sort can beat merge sort, and library sorts switch to insertion sort for short runs. As n grows, the class with the slower growth always wins in the end.

How big can n be?

Any whole number from 1 to 10¹⁵ for the table, and up to 1,000,000 for the chart. The step counts are worked out with logarithms, so even 1,000,000! steps, a number with about 5.6 million digits, is shown as a power of ten instead of overflowing.

Ready to try it?

Open PictorX in your browser — no install, no credit card required to start.

Go to Editor