Skip to content

how much work one step takes

foundations

the puzzle

Earlier lessons count each small act, such as one comparison, as one step. Is adding two numbers of 300 digits one step too?

a worked example

A computer keeps numbers in words, numbered boxes of memory, each made of 64 bits on most of today's computers.

  1. A bit is a digit that is 0 or 1. 3 bits make 8 patterns, 000 to 111, and in order they name the numbers 0 to 7, as the table shows.
  2. The table's 3 bits give 2×2×2=82 \times 2 \times 2 = 8 patterns, and 64 bits give 64 twos multiplied, the 2642^{64} numbers from 0 to 264−12^{64} - 1, about 18 billion billion, a number of 20 digits.
  3. Each of these is one step. Read a word, which brings its number out of memory to be worked on, or write a number into a word. Add, take away, multiply, divide or take the remainder of two numbers held in words. Compare two of them.
  4. To write the larger of places 5 and 9 into place 0, read place 5, read place 9, compare, and write. That is 4 steps.
3 bitsthe number
0000
0011
0102
0113
1004
1015
1106
1117
3 bits write 8 numbers, 0 to 7. Each more bit doubles the count.

how it works

Memory is counted in words too. A list of nn numbers, each small enough for one word, takes nn words.

bits in the numberwords of 64 bits
641
652
1282
2004
100016
One more bit past a word's 64 needs a whole second word.

A number of bb bits takes ⌈b64⌉\lceil \frac{b}{64} \rceil words, rounded up, as a part-filled word is still a whole word. For 200 bits, 200÷64=3.125200 \div 64 = 3.125, so ⌈3.125⌉=4\lceil 3.125 \rceil = 4 words.

10 bits write 210=10242^{10} = 1024 numbers (lesson 3.5), 0 to 1023, enough for any 3 digits. 300 digits are 100 groups of 3, so they need about 100×10=1000100 \times 10 = 1000 bits, which fill 16 words.

Adding two such numbers goes word by word, carrying as on paper, so it takes about 16 times the steps of one word. So adding two 300-digit numbers is not one step.

in symbols

A job's extra space is the words it uses beyond its input, the words holding what it is given. Adding up a list of small numbers into one total uses 2 extra words, the total and the counter, whatever nn is.

Making a reversed copy of the list uses n+1n + 1 extra words, the copy and a counter.

This way of counting is called the word-RAM model. RAM is short for random-access machine, a computer whose memory places can be read in any order.

It counts one step for each act on a word and one place for each word. Pat Morin's free textbook Open Data Structures describes it in section 1.4, with words of ww bits, and this page takes w=64w = 64.

job on a list of nn numbersinput wordsextra words
add them into one totalnn2
find the largestnn2
make a reversed copynnn+1n + 1
Only the copy's extra words grow with nn.

mind these

  • Counting steps measures how long a job takes. Counting words measures how much memory it uses.

your turn

Work each one out. Where there is a box, type your answer and check it. Each exercise's worked answer opens under it.

exercise 1

A job swaps the numbers in places 2 and 6 by three copies: place 2 into place 0, place 6 into place 2, then place 0 into place 6. Each copy reads one place and writes another.

A number, like 13, 9.5 or 19/2.

worked answer
  1. Copy place 2 into place 0, a read and a write, 2 steps.
  2. Copy place 6 into place 2, 2 more.
  3. Copy place 0 into place 6, 2 more, so 6 in all.

Check it another way: 3 copies, each a read and a write, make 6 steps.

exercise 2

A number has 150 bits. Each word holds 64 bits.

A number, like 13, 9.5 or 19/2.

worked answer
  1. 150÷64150 \div 64 is about 2.34.
  2. 2 words hold only 128 bits, so a third word is needed, ⌈2.34⌉=3\lceil 2.34 \rceil = 3.

Check it another way: 3 words hold 192 bits, enough for 150, and 2 hold 128, too few.

exercise 3

A list holds 500 numbers, each of 100 bits, in words of 64 bits. Each number starts in a word of its own.

A number, like 13, 9.5 or 19/2.

worked answer
  1. Each number needs ⌈10064⌉=2\lceil \frac{100}{64} \rceil = 2 words.
  2. 500 numbers take 500×2=1000500 \times 2 = 1000 words.

Check it another way: 1000 words hold 64000 bits, and 500 numbers of 100 bits fill 50000 of them, with each number's spare bits unused.

exercise 4

Three jobs each take a list of nn small numbers.

The job whose extra space grows with nn:

worked answer
  1. Adding into a total keeps a total and a counter, 2 words.
  2. Finding the largest keeps the largest so far and a counter, 2 words.
  3. A reversed copy needs a new place for each of the nn numbers and a counter, n+1n + 1 words.

Check it another way: Only the copy's extra words change when nn changes.

exercise 5

Adding two big numbers word by word takes 4 steps for each word of one number: 2 reads, one word of each, then an add and a write. Each number has 512 bits, in words of 64 bits.

A number, like 13, 9.5 or 19/2.

worked answer
  1. Each number fills 512÷64=8512 \div 64 = 8 words.
  2. At 4 steps for each of the 8 words, 8×4=328 \times 4 = 32 steps.

Check it another way: Twice the bits, 1024, would be 16 words and 64 steps, so the steps grow with the length of the numbers.