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.
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.
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 + 14print(c)
Model answer (lines are separated by /, and [indent] means four spaces at the start of the line): a = int(input("Number? ")) / print(a * a + 1). Check: The original prints 17, because 4 × 4 = 16 and 16 + 1 = 17. It uses three variables: a, b and c. The rewritten program also prints 17 and uses one variable.
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?
Line
Program Q
Program R
1
x = int(input("First? "))
x = int(input("First? "))
2
y = int(input("Second? "))
y = int(input("Second? "))
3
total = x + y
print(x + y)
4
diff = x - y
print(x - y)
5
print(total)
6
print(diff)
Program Q uses four variables (x, y, total and diff). Program R uses two (x and y). R is more efficient. Both print 13 and then 5.
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.
No. It prints 12, because it multiplies, but the total is 7. It does not meet the requirement, so its efficiency does not matter. Efficiency is compared only after the requirements are met.
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 U
Program V
length = float(input("Metres? "))
print(float(input("Metres? ")) * 100)
cm = length * 100
print(cm)
U is easier to read and check, because each name says what the value means, and the memory saving is tiny in a small program. A good programmer balances short code with code that other people can understand.