seegongsik
Saved words
Algorithms

The ruler that measures speed

How do you tell which way is faster? A stopwatch seems right, but on a fast computer everything looks fast. The real difference hides when there are few boxes, and shows up when the boxes pile up. So we measure speed not in seconds, but by "as the boxes grow, how much does the work grow?"

01

Count the steps

Seconds confuse us.
On a fast computer
even a slow way looks quick.
So instead of seconds,
we count "how many steps?"
When there are n boxes,
each way
takes a different number of steps.

Pick input size
One by onen steps
?
By halflog n steps
?
The bigger the boxes, the wider the gap between the two counts.

Pick an input size and count the steps each way takes. (One by one = n · by half = few)

With few boxes,
the two look similar.
For 8 boxes,
one by one is 8,
by half is 3.
Not much gap.
But what happens
when we grow the boxes a lot?

02

What happens as it grows

Here the truth shows.
Grow n, and
one way climbs
slowly, with the boxes,
while another,
when the boxes double,
jumps to four times the work.
n times n,
that is n squared.

2n4n times n
now n = 2 · n times n = 4
Each time the boxes double, n doubles but n times n jumps to four times.

Tap to grow n. n rises with the boxes, but n squared explodes as the boxes get bigger.

n squared is scary
when the boxes are big.
100 boxes:
n is 100,
n squared is 10,000.
1,000 boxes:
n is 1,000,
n squared is a million.
It starts small
but soon grows out of hand.

03

Compare the curves

Put three growths
side by side
and you see it at a glance.
log n
barely climbs
even as the boxes grow.
n rises in a straight slant,
n squared curves up
and shoots far above.

n
At the same n, n times n shoots up high while log n hugs the bottom.

Tap to turn on curves and compare. At the same n, log n, n, and n squared reach completely different heights.

Now you see
why cutting in half
was so fast.
By half is log n:
even a thousand boxes
finish in about ten.
One by one is n:
a thousand means a thousand.
The shape of the growth
decided the winner.

04

Keep only the biggest term

Count the real steps and
you get a messy formula
like 3n squared + 5n + 9.
But when n gets very big,
n squared
overwhelms the rest.
So we erase the small terms
and keep only
the fastest-growing, n squared.

3n2+ 5n+ 9
still terms left to erase
When n gets very big, small terms and the front number get buried. Keeping only the big term is big O.

Tap the small terms to erase them one by one. In 3n squared + 5n + 9, when n is big only n squared remains, and that is big O.

We drop the front number too.
3n squared or 100n squared,
the shape of the growth is the same n squared.
So we just write
O(n squared).
Big O is a name tag
that writes, in one line,
"in what shape does it grow for big inputs?"

05

Look how far we've come

To sum up:
speed is measured not in seconds
but by the shape of growth.
As the boxes n grow,
log n barely rises,
n rises with the boxes,
n squared explodes.
However messy the formula,
you look at just the biggest term,
and that mark is big O.

Tap a term to turn on its growth shape

Tap the three to sum it up in one line. log n nearly flat, n slanting, n squared bursting upward. Just look at the biggest term.

Now, looking at a method,
you can gauge its speed
without timing a second.
If doubling the boxes
doubles the work, fine;
if it leaps to four times, beware.
A good method
is one that grows slowly
even before a big job.

In one lineSpeed is measured not in seconds but by the shape of its growth. You look at how much the work grows as the input boxes n get bigger. log n barely grows, n grows with the boxes, n squared explodes. And however messy the formula, you keep just the one term that grows the most. 3n squared + 5n + 9 is simply n squared. This way of seeing only the biggest term is big O. That is why, with big data, cutting in half beats one by one beyond compare.
Algorithms
Was this helpful? Support seegongsik