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.
Step
Rule
1
Add the waits in one round.
2
Multiply by the number of rounds.
3
Compare the totals.
Worked examples
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.
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 X
Algorithm Y
Wait in one round
3 seconds
2 seconds
Rounds in all
2
4
X: 3 × 2 = 6 seconds. Y: 2 × 4 = 8 seconds. Y takes longer by 2 seconds.
2
How many seconds of waiting are there in all in this program?
9 seconds. One round waits 1 + 2 = 3 seconds, and there are 3 rounds.
3
How many times does the program in question 2 show the house icon?
3 times, once in each round.
4
Name one reason why a test with a real board or a stopwatch may not match the total you predicted.
For example: a sound or a scrolling message takes extra time, or the person with the stopwatch starts or stops it a little late.