counting basic steps
counting the cost
what it is
A method's cost is counted in steps, not seconds, since a count of steps belongs to the method alone.
Take a list of 4 short names, and a name to look for that is not in it. The method 'look at every name' looks at place 1, then 2, 3 and 4. At place 5 there is no name, so it says the name is not there and stops. The table writes it as four instructions, A to D.
| instruction | what it does | runs for 4 names | runs for names |
|---|---|---|---|
| A | start at place 1 | 1 | 1 |
| B | if there is no name at this place, say the name wanted is not there, and stop | 5 | |
| C | look at the name here: if it is the one wanted, say where it is, and stop | 4 | |
| D | move on to the next place, and go back to instruction B | 4 | |
| in all | 14 |
A method like this is an algorithm: clear steps that turn what it is given, its input, into what is wanted. Here the input is a list of names, at places 1 to , and the name wanted. Every count of the method on this page is for a list without the name wanted, where the method does the most it can.
A stopwatch would time the computer, the programming language and whatever else the computer is doing, as well as the method. Time it twice and you get two answers.
A basic step is one read from memory, one write, one small working out such as comparing two short names, or one choice of what comes next. Instruction C reads the place, the name there and the name wanted, compares the two names and chooses: 5 basic steps. Every instruction does a fixed few, from 1 to 5, the same however long the list.
Instructions run are easier to count than basic steps. For 4 names, 14 instructions run against 53 basic steps. The numbers differ, but question 4 shows the basic steps stay between the instructions run and 4 times them. So both grow the same way, and question 1 counts instructions: .
Memory is counted in cells, each holding one number or one short name. 'Look at every name' uses one cell besides its input, for the place it has reached, however long the list.
where the formulas come from
Each question is worked out one numbered line at a time. A line shows the move it makes, the line itself, and why the move is allowed. A line either follows from the lines above it, or brings in something the question needs, such as how the method works, a small fact about , or a result from another page, with its own reason. Press 'hide the lines, then guess each one' to cover the lines and uncover them one by one, guessing each before you see it.
the move: follow the walk
the line:why it is allowed: A starts at place 1. At each place with a name, B lets the method through, C looks and does not stop it, and D moves it on. At place , B finds no name and stops it.
the move: count instruction
the line:why it is allowed: A runs at the start, and nothing goes back to it.
guess it, thenthe move: count instruction
the line:where the method is B C D place 1 runs runs runs place 2 runs runs runs place 3 runs runs runs place 4 runs runs runs place 5 runs, and stops in all 5 4 4 A list of 4 names without the name wanted: B runs 5 times, C and D 4 times each. why it is allowed: B runs at every place visited, places 1 to by line 1.
guess it, thenthe move: count instruction
the line:why it is allowed: C runs at each place with a name, places 1 to by line 1.
guess it, thenthe move: count instruction
the line:why it is allowed: D runs after each run of C, since by line 1 C never stops the method.
guess it, thenthe move: add them up
the line:why it is allowed: Let count every run of every instruction. Add lines 2 to 5.
guess it, thenthe move: add like with like
the line:why it is allowed: The three lots of make , and the two 1s make 2. For 4 names, 14.
guess it, thenthe move: a fact for the ceiling
the line:why it is allowed: Another way of counting gives another exact formula. So trap between two multiples of . First a fact: is at least 1, so 2 is at most .
guess it, thenthe move: a ceiling
the line:why it is allowed: By line 8, swap the 2 in line 7 for , which is no smaller.
guess it, thenthe move: add like with like
the line:why it is allowed: and make .
guess it, thenthe move: think of at every length at once
the line:names instructions run 5 times the names 1 5 5 2 8 10 4 14 20 10 32 50 100 302 500 At every length, the count stays at or under 5 times the names. why it is allowed: Lines 1 to 10 hold for every list of at least one name. So read as the whole graph of counts against lengths, not one point.
guess it, thenthe move: read it as a bound
the line:names twice the names and 20 more 3 times the names 1 22 3 10 40 30 20 60 60 30 80 90 100 220 300 A made-up count breaks the ceiling of 3 times the names below 20 names and meets it at 20. Each name more adds 2 to the count and 3 to the ceiling. why it is allowed: is the group of counts that, from some length on, never climb above a fixed number above zero times . Short lists may break it, as in the table beside this line. By line 10, 5 serves for . Read is in big O of .
guess it, thenthe move: a floor
the line:- the count of instructions run,
- the ceiling of line 10,
- the floor of this line,
A log-log plot: each step across or up is ten times the one before. Multiplying by the same number moves up the same distance. So a line always the same multiple of another stays the same distance above it: 50 sits as far above 30 as 500 above 300. The count, the heavy line, stays between and . why it is allowed: Leave the 2 out of line 7. What is left is smaller.
guess it, thenthe move: read it as a bound
the line:why it is allowed: is the same with a floor: from some length on, never below a fixed number above zero times . By line 13, 3 serves for . Read is in big omega of .
guess it, thenthe move: a ceiling and a floor together
the line:why it is allowed: By lines 10 and 13, stays between and ; lines 12 and 14 read both as bounds with the same term, . That is the tight bound, read is in theta of : grows like . Counting the basic steps gives 3 and 20 instead (question 4); the term stays .
guess it, then
try it with a small : at , line 6 gives , which you can work out by hand
the move: count the looks
the line:why it is allowed: Each run of C is one look at a name. By line 4 of question 1, C runs times.
the move: instructions run for each look
the line:names instructions run looks instructions for each look 1 5 1 5 2 8 2 4 4 14 4 3.5 1,000 3,002 1,000 3.002 Each look costs 5 instructions at 1 name, and nearer 3 as the list grows. why it is allowed: Let be the instructions run for each look. By line 7 of question 1, of them are shared among looks.
guess it, thenthe move: divide each part
the line:why it is allowed: shared among is 3, and 2 shared among is .
guess it, thenthe move: a floor
the line:why it is allowed: is above zero, so leaving it out of line 3 leaves less.
guess it, thenthe move: a fact for the ceiling
the line:why it is allowed: is at least 1, so 2 shared among is at most 2.
guess it, thenthe move: a ceiling
the line:why it is allowed: By line 5, swap the in line 3 for 2, which is no smaller.
guess it, thenthe move: add them
the line:- the count of instructions run,
- the looks,
The instructions run and the looks on a log-log plot. The first stays 3 to 5 times the second, so the two lines run side by side. why it is allowed: 3 and 2 make 5.
guess it, thenthe move: read the two together
the line:why it is allowed: By lines 4 and 7, each look costs 3 to 5 instructions. So no: counted by its looks, the cost still grows like .
guess it, then
try it with a small : at , line 2 gives , which you can work out by hand
the move: put in twice as many names
the line:why it is allowed: Let be the instructions run for names, twice as many. Put for in line 7 of question 1.
the move: multiply out
the line:why it is allowed: 3 lots of make .
guess it, thenthe move: twice the instructions for n names
the line:why it is allowed: Twice is , and twice 2 is 4.
guess it, thenthe move: write 2 as 4 take away 2
the line:why it is allowed: Line 2, its 2 written as 4 take away 2.
guess it, thenthe move: use line 3
the line:names instructions run twice those instructions run for twice the names 4 14 28 26 10 32 64 62 1,000 3,002 6,004 6,002 Twice the names always run 2 fewer than twice the instructions. why it is allowed: By line 3, is twice .
guess it, thenthe move: read it for a long list
the line:why it is allowed: The 2 matters less as the list grows: 2,000 names run 6,002 instructions, against twice 3,002, 6,004. The looks double exactly, one for each name.
guess it, then
try it with a small : at , line 1 gives , which you can work out by hand
the move: count each instruction's basic steps
the line:instruction its basic steps how many A write 1 into the cell for the place reached 1 B read the place and the list's length, compare them (past the length there is no name), choose 4 C read the place, the name there and the name wanted, compare the two names, choose 5 D read the place, add one, write it back 3 Each instruction's basic steps, the same however long the list. Saying the answer is not counted. why it is allowed: One step for each read, write, working out and choice, as in the table. Going on to the only next instruction is no choice, so it is free. Others might count one more or less; line 16 says why that hardly matters.
the move: each instruction's steps times its runs
the line:why it is allowed: Steps from line 1, runs from lines 2 to 5 of question 1.
guess it, thenthe move: add them up
the line:why it is allowed: Let be the basic steps. Add the four products of line 2.
guess it, thenthe move: multiply out the bracket
the line:why it is allowed: Four times is and 4.
guess it, thenthe move: add like with like
the line:why it is allowed: , and make , and 1 and 4 make 5. For 4 names, 53.
guess it, thenthe move: a fact for the floor
the line:why it is allowed: The aim: the basic steps sit between the instructions run and 4 times them. For the floor, a fact: is and more, and is above zero.
guess it, thenthe move: a floor
the line:why it is allowed: By line 6, swap the in line 5 for , which is no bigger.
guess it, thenthe move: a lower floor
the line:why it is allowed: Swap the 5 in line 7 for 2, which is smaller. is the instructions run, by line 7 of question 1, so there are never fewer basic steps.
guess it, thenthe move: a ceiling
the line:why it is allowed: 8 is more than 5. Swap the 5 in line 5 for 8.
guess it, thenthe move: four times the instructions run
the line:why it is allowed: Four times is , and four times 2 is 8.
guess it, thenthe move: use line 10
the line:names instructions run basic steps 4 times the instructions run 1 5 17 20 4 14 53 56 10 32 125 128 100 302 1,205 1,208 The basic steps always sit between the instructions run and 4 times them. why it is allowed: By line 10, line 9 says at most four times the instructions run.
guess it, thenthe move: the ceiling of question 1
the line:why it is allowed: By lines 7 and 10 of question 1, the instructions run are never more than .
guess it, thenthe move: a ceiling in n alone
the line:why it is allowed: By line 12, swap the in line 11 for , which is no smaller.
guess it, thenthe move: multiply
the line:why it is allowed: 4 times is .
guess it, thenthe move: a floor in n alone
the line:- the basic steps,
- the ceiling of line 14,
- the floor of this line,
The basic steps stay between and , on a log-log plot as in question 1. why it is allowed: Leave the 2 out of line 8. What is left is smaller.
guess it, thenthe move: read them together
the line:why it is allowed: By lines 8 and 11, the basic steps sit between the instructions run and 4 times them. Count B as 5 steps instead: for 4 names B runs 5 times, so 5 steps more, 58, still between 14 and 5 times 14, 70. Every instruction does at least 1 basic step and at most a fixed few, here 5. So any such count stays between , question 1's instructions run, and a fixed multiple of it. Here lines 14 and 15 put between and : it grows like .
guess it, then
try it with a small : at , line 3 gives , which you can work out by hand
a worked example
Count the instructions run, and the looks, for a name that is not in a list of 4 names.
- A starts at place 1: 1 instruction.
- At each of places 1 to 4, B, C and D run once: 3 instructions a place, 12 in all, and 4 looks.
- At place 5 there is no name. B runs once more and stops: 1 instruction.
- 1, 12 and 1 make 14. gives 14 for 4 names too: 3 times 4 is 12, and 2 more is 14.
the trap
check yourself
Two people time the same method and get different numbers. Two others count its instructions run and get the same. Why?
the answer
A time also measures the computer, the language and the moment. A count belongs to the method alone.
One person counts instructions run, another only the looks. Do they disagree about how the cost grows?
the answer
No. Each look costs 3 to 5 instructions, so the two counts grow together, like .
How many memory cells does 'look at every name' use besides its input, for 1,000 names, and for a million?
the answer
One each time, for the place it has reached. Its memory stays the same however long the list is.