Skip to main content

Growth Rates: What Complexity Means in Practice

11 min readRead first: Asymptotic Notation

Asymptotic notation gives us a precise language. This lesson turns it into practical intuition: what the most common complexity classes mean in real time, how to predict the time an algorithm will need for a larger input, how to estimate the largest input it can handle in a given time, and why no improvement in hardware can change the complexity of an algorithm.

Asymptotic analysis allows us:

  • to measure the inherent difficulty of a problem;
  • to estimate the time and space required by an algorithm before running it;
  • to compare algorithms that solve the same problem in terms of efficiency.
What you will learn in this lesson
  • The main complexity classes, from O(1)O(1) to O(nn)O(n^n), and which ones are acceptable in practice.
  • How dramatically the classes differ when nn grows, and where the "small nn" exceptions are.
  • How to calculate the expected execution time for a new problem size, and the largest problem size solvable in a given time.
  • The principle of invariance and its consequences.

The Basic Complexity Classes​

ClassNameCommentTypical example
O(1)O(1)ConstantOptimal, but almost always impossible for problems that must look at their input.Accessing an element of an array by position.
O(log⁡n)O(\log n)LogarithmicExcellent: the time barely grows.Binary search in a sorted array.
O(n)O(n)LinearA good and quite usual complexity. Often optimal, since reading the input already takes nn steps.Sequential search; maximum of a vector.
O(nlog⁡n)O(n \log n)LinearithmicA good complexity, typical of efficient recursive (divide and conquer) algorithms.Mergesort; Quicksort on average.
O(n2)O(n^2)QuadraticAppears with two nested loops and similar cases. Acceptable for moderate sizes.Simple sorting algorithms (bubble, insertion, selection).
O(n3)O(n^3)CubicGrows excessively for large values.Naive multiplication of matrices.
O(nk)O(n^k)PolynomialGeneral case of polynomial complexity, for a constant kk.Checking all kk-element subsets of a set; kk nested loops.
O(2n)O(2^n)ExponentialAvoid it if possible.Generating all the subsets of a set.
O(kn)O(k^n)ExponentialGeneral case of exponential complexity, for a constant k>1k \gt 1.Graph kk-coloring by exhaustive search.
O(n!)O(n!)FactorialA bad complexity; it should be avoided.Generating all the permutations of nn elements.
O(nn)O(n^n)Super-exponentialA bad complexity; it should be avoided.Generating all nn-tuples over nn values (all functions from a set of size nn to itself).

The classes up to O(nk)O(n^k) are polynomial; from O(2n)O(2^n) onwards they are not polynomial. This border is very significant in computer science: problems that can be solved in polynomial time are usually considered tractable, while those that seem to require exponential time are considered intractable for large inputs. Some of the most famous open questions in computer science (such as whether P equals NP) are about exactly this border. Several design techniques of this course, such as backtracking and branch and bound, exist precisely to deal with problems for which no polynomial algorithm is known, trying to avoid exploring the whole exponential search space.


How Fast Do They Grow?​

The following table shows the time that an algorithm would need for different input sizes, assuming that each elementary operation takes one nanosecond (a billion operations per second, a reasonable order of magnitude for a modern processor executing simple instructions):

nnlog⁡2n\log_2 nnnnlog⁡2nn \log_2 nn2n^2n3n^32n2^nn!n!
103 ns10 ns33 ns100 ns1 µs1 µs4 ms
204 ns20 ns86 ns400 ns8 µs1 ms77 years
506 ns50 ns282 ns2.5 µs125 µs13 days104710^{47} years
1007 ns100 ns664 ns10 µs1 ms101310^{13} years1014110^{141} years
1,00010 ns1 µs10 µs1 ms1 s1028410^{284} years—
10,00013 ns10 µs133 µs100 ms17 min——
1,000,00020 ns1 ms20 ms17 min32 years——
Growth of log n, n, n log n, n squared and 2 to the n for n up to 20noperations0510152002550751002ⁿn²n log₂ nnlog₂ nWith n = 20: log₂ n ≈ 4, n = 20, n log₂ n ≈ 86, n² = 400, 2ⁿ ≈ 1,000,000
The basic complexity classes for n up to 20. Even at this tiny scale, 2ⁿ and n² leave the chart while n log n, n and log n stay close to the axis.

For comparison, the age of the universe is about 1.4×10101.4 \times 10^{10} years. Some observations:

  • Logarithmic algorithms are practically insensitive to the size: going from a thousand to a million elements only doubles the time.
  • Linear and linearithmic algorithms handle millions of elements in milliseconds.
  • Quadratic algorithms are fine for thousands of elements but take minutes for millions.
  • Exponential and factorial algorithms are only usable for very small inputs. Note that 2n2^n goes from a millisecond to 13 days when nn goes from 20 to 50.

How large a problem can we solve in one second?​

Reversing the question gives a very practical rule of thumb. With about 10910^9 simple operations per second:

ComplexityLargest nn in about one second
O(n)O(n)about 10910^9
O(nlog⁡n)O(n \log n)about 4×1074 \times 10^7
O(n2)O(n^2)about 30,00030{,}000
O(n3)O(n^3)about 1,0001{,}000
O(2n)O(2^n)about 3030
O(n!)O(n!)about 1212

Real programs perform several machine instructions per "operation", access memory, allocate objects... so these limits are optimistic, but the orders of magnitude are very useful. Competitive programmers use exactly this kind of table to decide, from the size of the input, which complexity their solution needs.

Small values of nn​

The asymptotic order describes what happens for large nn, and for small values the picture can be surprising. Compare, for example, log⁡2n\log_2 n with n0.4n^{0.4}, or nlog⁡2nn \log_2 n with n1.4n^{1.4} (which is the same comparison multiplied by nn):

nnn0.4n^{0.4}log⁡2n\log_2 nn0.6n^{0.6}nnn1.4n^{1.4}nlog⁡2nn \log_2 nn1.6n^{1.6}n2n^2
21.321.001.5222.642.03.034
82.303.003.48818.424.027.964
163.034.005.281648.564.084.4256
1607.617.3221.01601,2181,1723,36225,600
1,28017.510.373.21,28022,39213,21293,6561,638,400
41,943,0401,12025.337,46241,943,0404.7×10104.7 \times 10^{10}1.1×1091.1 \times 10^{9}1.6×10121.6 \times 10^{12}1.8×10151.8 \times 10^{15}

For small nn, log⁡2n\log_2 n is larger than n0.4n^{0.4} and nlog⁡2nn \log_2 n is larger than n1.4n^{1.4}. The polynomial functions only overtake the logarithmic ones from about n=132n = 132, and from then on the gap grows without limit. Asymptotic conclusions are always about the long run.


Predicting Execution Times​

Suppose that a program with complexity O(f(n))O(f(n)) takes t1t_1 seconds for a problem of size n1n_1. How long will it take for a larger size n2n_2?

Since the time is (approximately) proportional to f(n)f(n), with the same constant for both sizes:

t2t1=f(n2)f(n1)⟹t2=f(n2)f(n1)⋅t1\frac{t_2}{t_1} = \frac{f(n_2)}{f(n_1)} \qquad\Longrightarrow\qquad t_2 = \frac{f(n_2)}{f(n_1)} \cdot t_1

Writing n2=k⋅n1n_2 = k \cdot n_1, with k=n2/n1k = n_2 / n_1, we obtain simple formulas for the most common classes:

f(n)f(n)t2t_2Explanation
nnk⋅t1k \cdot t_1The time grows in the same proportion as the size.
ncn^ckc⋅t1k^c \cdot t_1Doubling the size multiplies the time by 2c2^c.
log⁡n\log nlog⁡n2log⁡n1⋅t1\dfrac{\log n_2}{\log n_1} \cdot t_1Any base can be used, as long as it is the same in both logarithms.
nlog⁡nn \log nk⋅log⁡n2log⁡n1⋅t1k \cdot \dfrac{\log n_2}{\log n_1} \cdot t_1A little more than linear.
nc1log⁡c2nn^{c_1} \log^{c_2} nkc1⋅(log⁡n2log⁡n1)c2⋅t1k^{c_1} \cdot \left(\dfrac{\log n_2}{\log n_1}\right)^{c_2} \cdot t_1Combination of the previous ones.
cnc^nc n2−n1⋅t1c^{\,n_2 - n_1} \cdot t_1Depends on the difference of the sizes, not on their ratio.
n!n!n2!n1!⋅t1=(n1+1)(n1+2)⋯n2⋅t1\dfrac{n_2!}{n_1!} \cdot t_1 = (n_1 + 1)(n_1 + 2) \cdots n_2 \cdot t_1Grows even faster than exponential.

For example, if a program takes 1 unit of time for n1=1,000n_1 = 1{,}000:

Complexityn2=10,000n_2 = 10{,}000n2=106n_2 = 10^6
O(1)O(1)11
O(log⁡n)O(\log n)≈ 1.32
O(n)O(n)101,000
O(nlog⁡n)O(n \log n)≈ 13≈ 2,000
O(n2)O(n^2)10010610^6
O(n2log⁡n)O(n^2 \log n)≈ 133≈ 2×1062 \times 10^6
O(n3)O(n^3)1,00010910^9
O(2n)O(2^n)290002^{9000}29990002^{999000}
O(n!)O(n!)∏i=100110000i\prod_{i=1001}^{10000} i∏i=1001106i\prod_{i=1001}^{10^6} i
🧠 Checkpoint: Predict the time

Question: A cubic algorithm takes 2 seconds for n=500n = 500. How long will it take for n=2,000n = 2{,}000? And an O(nlog⁡2n)O(n \log_2 n) algorithm that takes 2 seconds for n=1,024n = 1{,}024, for n=4,096n = 4{,}096?

Answer: For the cubic one, k=4k = 4 and t2=43⋅2=128t_2 = 4^3 \cdot 2 = 128 seconds. For the linearithmic one, k=4k = 4 and log⁡24096log⁡21024=1210=1.2\frac{\log_2 4096}{\log_2 1024} = \frac{12}{10} = 1.2, so t2=4⋅1.2⋅2=9.6t_2 = 4 \cdot 1.2 \cdot 2 = 9.6 seconds.


Predicting the Largest Solvable Problem​

The reverse question is just as useful: if a program with complexity O(f(n))O(f(n)) solves a problem of size n1n_1 in time t1t_1, what size n2n_2 can it solve in a larger time t2t_2? (Equivalently: what size could it solve in the same time on a computer kk times faster?)

With t2=k⋅t1t_2 = k \cdot t_1, i.e. k=t2/t1k = t_2 / t_1:

f(n2)=t2t1⋅f(n1)=k⋅f(n1)⟹n2=f−1(k⋅f(n1))f(n_2) = \frac{t_2}{t_1} \cdot f(n_1) = k \cdot f(n_1) \qquad\Longrightarrow\qquad n_2 = f^{-1}\big(k \cdot f(n_1)\big)
f(n)f(n)n2n_2Effect of kk times more time (or a kk times faster computer)
nnk⋅n1k \cdot n_1The size grows by the same factor kk.
ncn^ck1/c⋅n1k^{1/c} \cdot n_1The size grows only by kc\sqrt[c]{k}.
log⁡n\log nn1 kn_1^{\,k}The size grows enormously.
cnc^nn1+log⁡ckn_1 + \log_c kThe size only increases by a constant amount.

For example, if a program solves a problem of size n1=10n_1 = 10 in 1 unit of time:

ComplexityWith 1,000 units of timeWith 10610^6 units of time
O(1)O(1)any sizeany size
O(log⁡2n)O(\log_2 n)10100010^{1000} (≈23322\approx 2^{3322})10100000010^{1000000}
O(n)O(n)10410^410710^7
O(n2)O(n^2)≈ 31610410^4
O(n3)O(n^3)1001,000
O(2n)O(2^n)≈ 20≈ 30

The last row is the most important one: with an exponential algorithm, a computer a million times faster only lets us go from 10 to about 30 elements. Exponential complexity cannot be defeated with hardware.

🧠 Checkpoint: A faster computer

Question: An O(n2)O(n^2) program processes 5,000 records per night. The company buys a server 16 times faster. How many records can it process per night now? And if the program were O(2n)O(2^n) and processed 40 records?

Answer: For the quadratic program, n2=16⋅5,000=20,000n_2 = \sqrt{16} \cdot 5{,}000 = 20{,}000 records: 16 times more power, but only 4 times more data. For the exponential one, n2=40+log⁡216=44n_2 = 40 + \log_2 16 = 44 records: a 16 times faster server only adds 4 records.


The Principle of Invariance​

Given an algorithm of complexity O(f(n))O(f(n)), it will always have that complexity, regardless of the language in which it is programmed, the quality of the compiler, the speed of the computer, etc. These factors affect the implementation constant, but never the complexity O(f(n))O(f(n)).

More formally: two different implementations of the same algorithm, on any two machines, will never differ in efficiency by more than a constant factor. If one takes t1(n)t_1(n) and the other t2(n)t_2(n), there is a constant cc such that t1(n)≤c⋅t2(n)t_1(n) \le c \cdot t_2(n) for large enough nn.

This principle has three important consequences:

  1. It justifies the asymptotic approach. Since languages, compilers and machines only change the constant, studying the order of growth tells us something true about the algorithm itself, valid on any computer, present or future.
  2. It explains the time ratios of the lab. When we compare the same algorithm in two environments (for example, Python and Java), the ratio of their times tends to a constant: the implementation constant.
  3. It sets the limits of hardware. Better hardware divides the time by a constant. As the previous tables show, that can compensate for a better algorithm only for small inputs: for large inputs, the algorithm with the better growth rate always wins.
Common mistakes
  • Using the ratio of sizes to predict a time. The time scales with f(n2)f(n1)\frac{f(n_2)}{f(n_1)}, not with n2n1\frac{n_2}{n_1}, unless the algorithm is linear.
  • Expecting faster hardware to rescue an exponential algorithm. It only adds a constant to the size you can solve.
  • Comparing algorithms at one small size. Growth shows up for large nn; at small sizes the constants may hide it.

Summary​

  • The common complexity classes are, from best to worst: O(1)O(1), O(log⁡n)O(\log n), O(n)O(n), O(nlog⁡n)O(n \log n), O(n2)O(n^2), O(n3)O(n^3), O(nk)O(n^k), O(2n)O(2^n), O(n!)O(n!) and O(nn)O(n^n). Polynomial classes are considered tractable; exponential ones are not.
  • With about 10910^9 operations per second, a quadratic algorithm handles tens of thousands of elements per second, an exponential one only a few dozen.
  • To predict times: t2=f(n2)f(n1)⋅t1t_2 = \frac{f(n_2)}{f(n_1)} \cdot t_1. To predict sizes: n2=f−1(t2t1⋅f(n1))n_2 = f^{-1}\left(\frac{t_2}{t_1} \cdot f(n_1)\right).
  • For exponential algorithms, more time or faster hardware only adds a constant to the solvable size.
  • The principle of invariance: implementation, language and hardware change only the constant, never the complexity.

Practice​

  • ⏱️ Predict times. An O(n2log⁡2n)O(n^2 \log_2 n) algorithm takes 3 seconds for n=1,024n = 1{,}024. Estimate its time for n=4,096n = 4{,}096 and n=16,384n = 16{,}384.
  • 📏 Predict sizes. An algorithm takes 1 minute for n=2,000n = 2{,}000. What is the largest nn it can process in 1 hour if it is O(n)O(n)? And if it is O(n3)O(n^3)? And O(3n)O(3^n), if it took 1 minute for n=20n = 20?
  • 🧪 Check with experiments. Take one of the algorithms you measured in the lab sessions. From the time of one size, predict the time for a size 4 times larger, then measure it. How close was the prediction? What could explain the difference?
  • 🗺️ Choose a complexity. For each input size, which is the highest complexity that would still solve the problem in about one second: n=15n = 15, n=500n = 500, n=105n = 10^5, n=108n = 10^8?
Saved only in your browser
Was this lesson helpful?
Questions, or something wrong?

Errors, suggestions and public questions open a form on GitHub, visible to everyone.