AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Break it into parts

Week 18 · Unit 8: Be an innovator · 3 lessons
3

Which takes longer?

Computing 4: Unit 8, pp. 104–122
GoalPredict how long an algorithm takes by adding up its waits, and compare two algorithms.
New words
predict · to say what will happen before it happenstotal · the sum of all the parts added togethersimulator · a program on a computer that shows how the micro:bit would behave
Explanation

Before you run a program, predict what it will do. To predict how long a flashing algorithm takes, add up the waits in one round. Then multiply by the number of rounds. To compare two algorithms, work out the total for each and see which is bigger. A simulator on a computer shows how the board would behave, so it is a safe way to test a program before it goes onto a real board. Then compare the result with your prediction. A sound or a scrolling message also takes time, so a real test can take a little longer than your total. If the results are very different, find out why.

StepRule
1Add the waits in one round.
2Multiply by the number of rounds.
3Compare the totals.
Worked examples
0246810turn leftstopseconds in all
Compare the two bag-light algorithms. Turn left: (0.5 + 0.5) × 6 = 6 seconds. Stop: (2 + 1) × 3 = 9 seconds. The stop algorithm takes longer, even though it flashes fewer times.
play soundgiggleuntil doneshow iconheartpause (ms)1000clear screenpause (ms)2000repeat4timeson buttonApressed
A round can also play a sound. This round shows a heart, waits 1 second, clears the screen and waits 2 seconds. One round has 1 + 2 = 3 seconds of waiting. For 4 rounds the waiting is 3 × 4 = 12 seconds, plus the time the sound takes.
A prediction and a test do not have to match exactly. A stopwatch test might show 9.4 seconds for the stop algorithm, which was predicted as 9 seconds. Starting and stopping a stopwatch by hand is never exact.
Class activity

Stopwatch test (with a friend): follow the stop algorithm by hand. One person holds up a square card for 2 seconds, hides it for 1 second, and does this 3 times in all. The other person times the whole thing with a clock or a watch. Before you start, write your prediction. How close was it? Why is it not exactly the same? Now try the turn-left algorithm and compare the two times.

Practice
1
Algorithm X waits 3 seconds in each round and does 2 rounds in all. Algorithm Y waits 2 seconds in each round and does 4 rounds in all. Which one takes longer and by how much?
Algorithm XAlgorithm Y
Wait in one round3 seconds2 seconds
Rounds in all24
2
How many seconds of waiting are there in all in this program?
show iconhousepause (ms)1000clear screenpause (ms)2000repeat3timeson buttonBpressed
3
How many times does the program in question 2 show the house icon?
4
Name one reason why a test with a real board or a stopwatch may not match the total you predicted.