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

Which is better?

Week 9 · 1.6 Comparing algorithms · 3 lessons
3

A fair verdict

Computing 9: Topic 1.6, pp. 119–127
GoalCompare two programs in the right order (requirements first, then efficiency) and write a verdict with reasons.
New words
verdict · your final decision about which program is better, with reasonspass · one run of the body of a loopjustify · to give reasons that support a decision
Explanation

A fair comparison has an order. First, list the requirements and check that each program meets them. Remove any program that fails. Second, compare the efficiency of the programs that are left. Count the variables, because they use memory. You can also count the passes through a loop, because a program with fewer passes finishes sooner. Third, write your verdict and give reasons, such as ‘B is better because it has the same output but one variable fewer’. Sometimes the answer is mixed, for example when one program is more robust and the other is more efficient. Then say which matters more for the job. Experts have a more advanced method of measuring efficiency, called Big O notation, which you may meet later.

Worked examples
Python
1for n in range(5, 31, 5):
2    print(n)
Output
5
10
15
20
25
30
Two programs that print the multiples of 5 up to 30. Program A lets the counter jump in steps of 5. It makes 6 passes and uses one variable, n. The printed output is 5, 10, 15, 20, 25, 30, one number per line.
Python
1step = 0
2for n in range(1, 31):
3    step = step + 1
4    if step == 5:
5        print(n)
6        step = 0
Output
5
10
15
20
25
30
Program B prints exactly the same numbers. It looks at every number from 1 to 30, adds 1 to step each time, and prints when step reaches 5. It makes 30 passes and uses two variables, n and step.
CheckProgram AProgram B
Meets the requirementyesyes
Variables12
Passes through the loop630
Verdictbetterworse
The verdict. Both programs meet the requirement, so the efficiency can be compared. A is better on both counts. It uses fewer variables and makes fewer passes, so it uses less memory and finishes sooner.
Class activity

Water the plants (no computer). Five pots stand in a row, and the tap is at one end. Method A: fill a cup at the tap, water one pot, and walk back to the tap for the next cup. Method B: fill a jug at the tap once and water all five pots from it. Write each method as numbered steps. Act both out, with a pencil for a jug, and count the trips to the tap. Check that both methods meet the requirement that each pot gets water once. Then write a verdict of two sentences with a reason, and say which loop would have more passes if each method were a program.

Practice
1
The requirements are: ask for the number of apples and the number of baskets, and output how many apples go in each basket. Compare the programs. Which meets the requirements? Which is more robust? Which uses fewer variables? Give a verdict.
Python
1# Program L
2apples = int(input("Apples? "))
3baskets = int(input("Baskets? "))
4each = apples / baskets
5print("Each basket:", each)
6
7# Program M
8apples = int(input("Apples? "))
9baskets = int(input("Baskets? "))
10if baskets == 0:
11    print("Need at least 1 basket")
12else:
13    print("Each basket:", apples / baskets)
2
Both programs print 3, 6, 9, 12 and 15, one per line. How many passes does each loop make, and how many variables does each program use? Which is more efficient?
Python
1# Program J
2for n in range(3, 16, 3):
3    print(n)
4
5# Program K
6next_multiple = 3
7for n in range(1, 16):
8    if n == next_multiple:
9        print(n)
10        next_multiple = next_multiple + 3
3
Put these steps of a fair comparison in the correct order: write the verdict; compare the efficiency of the programs that work; list the requirements; check each program against the requirements.
4
Three programs have all been tested. Write a verdict. Which program do you choose, and why? Which program can you not choose?
ProgramRequirements metRobust?Variables
P3 of 3no5
Q3 of 3yes4
R2 of 3no1