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
2

Spend less memory

Computing 9: Topic 1.6, pp. 119–127
GoalCompare two working programs by counting the variables they use, and explain why fewer can be more efficient.
New words
efficiency · how little effort, memory or time a program needs to do its jobmemory · the part of the computer that holds data while a program runsdata structure · a way of storing data, such as a variable, a constant or a list
Explanation

Once two programs both meet the requirements, you can compare how efficient they are. One way to measure efficiency is memory. Every variable you create takes a small space in memory, and so does every constant and every list. If a variable is only needed for one line, the program may not need it at all. In a small program the saving is tiny. In a huge program, such as a game that stores and changes thousands of values every second, the savings add up. Efficiency only matters for programs that work, so always check the requirements first.

ProgramVariables it createsMemory used
Fewer variablesa fewless
Many variablesmanymore
Worked examples
Python
1price = float(input("Price? "))
2tax = price * 0.2
3total = price + tax
4print(total)
Output
48.0
Program A adds tax of 20 % to a price. The user types 40. It creates three variables: price, tax and total. It prints 48.0.
Python
1price = float(input("Price? "))
2print(price * 1.2)
Output
48.0
Program B does the same job with one variable. The expression price * 1.2 works out the tax and the total in one go. For the input 40 it prints 48.0 too, so both programs meet the requirements. B is more efficient with memory because it creates two fewer variables.
Python
1# Program C
2a = input("Like rice? y/n ")
3b = input("Like rice? y/n ")
4c = input("Like rice? y/n ")
5count = 0
6if a == "y":
7    count = count + 1
8if b == "y":
9    count = count + 1
10if c == "y":
11    count = count + 1
12print(count)
13
14# Program D
15count = 0
16for p in range(3):
17    vote = input("Like rice? y/n ")
18    if vote == "y":
19        count = count + 1
20print(count)
Counting votes. Program C stores three answers in three variables and has a fourth variable for the count, so it uses four. Program D handles one answer at a time, reusing the variable vote, so it uses three: count, p and vote. The user types y, n, y, and both print 2. With 30 voters, C would need 31 variables, but D would still need only three.
Class activity

Memory cups (no computer). Use paper cups or small boxes as memory spaces and slips of paper as values. Take program A from this lesson. Each time the program creates a variable, label a cup and put in its value. Run it with the input 40 and count the cups. Then run program B with a fresh set of cups and count again. Now try a three-line program of your own with a variable you could remove, and show that your shorter version needs fewer cups.

Practice
1
The user types 4. What does this program print? How many variables does it use? Rewrite it with only one variable.
Python
1a = int(input("Number? "))
2b = a * a
3c = b + 1
4print(c)
2
The requirements are: ask for two whole numbers, print their total, then print the first number minus the second. Both programs meet them. How many variables does each program use, and which is more efficient? What do they print for 9 and 4?
LineProgram QProgram R
1x = int(input("First? "))x = int(input("First? "))
2y = int(input("Second? "))y = int(input("Second? "))
3total = x + yprint(x + y)
4diff = x - yprint(x - y)
5print(total)
6print(diff)
3
The requirement is: ask for two whole numbers and print their total. The user types 3 and then 4. This program creates no variables at all. Can you say it is the most efficient program for the job? Explain.
Python
1print(int(input("First? ")) * int(input("Second? ")))
4
Program U names every step. Program V uses one line. Both are correct. Give one reason someone might still choose U, even though V uses less memory.
Program UProgram V
length = float(input("Metres? "))print(float(input("Metres? ")) * 100)
cm = length * 100
print(cm)