← VCE Software Development U3O1

Code optimisation

Changing code so it runs faster, uses less memory or is easier to read, without changing what it does. For example, removing repeated code or using a built-in function.

Understanding Code optimisation in VCE Software Development U3O1

What it does: Binary search finds the middle of the remaining range each time and halves the search space. This is a better algorithm than a linear search through every item. A descriptive name like average_mark makes the code easier to read and keeps the same behaviour, which shows the readability side of optimisation. Optimisation changes how the code works internally, not what it does. If the output changes, it is no longer optimisation. Built-in functions like sum() are tested, short and usually faster than hand-written code. They also work for any list length. Always test after optimising. The same test cases should still pass.

Common mistakes: len() needs a list, but total is a single number, so this causes a TypeError. Using the built-in sum() is a good optimisation, but the average must divide by len(marks). len(names) is a number and cannot be looped over, so this causes a TypeError. A loop replaces repeated print lines, but it must loop over the list itself: for name in names:.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 178–193) and word search (Modules 168–193).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which best describes code optimisation?
Improving speed, memory use or readability without changing what the program doesAdding new features so the program does more
Matches
for i in range(len(names)): print(len(names))
↓
len(names) is recalculated on every pass of the loop instead of being stored once in a variable
Applying
Meanings
Term
optimisation
Meaning
Changing code so it runs faster, uses less memory or is easier to read, without changing what it does. For example, removing repeated code or using a built-in function.
Practice
marks = [72, 85, 90, 64, 78]
Testing
Gaps
total = (marks)
Check Answer
Spots
2total = sum(marks)
3average = total / len(total)

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Gila Monster' badge — Patient Coder, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which best describes code optimisation?
A. Adding new features so the program does more
B. Improving speed, memory use or readability without changing what the program does ✓
Optimisation changes how the code works internally, not what it does. If the output changes, it is no longer optimisation.
2. A program has the same 4 lines copied three times. What is the best optimisation?
A. Add a comment above each copy of the lines
B. Move the repeated lines into a function and call it three times ✓
Removing repeated code makes a program shorter and easier to maintain. A fix is made in one place only.
3. Which is the better way to add up the numbers in a list called scores?
A. total = sum(scores) ✓
B. Write out scores[0] + scores[1] + scores[2] + ... by hand
Built-in functions like sum() are tested, short and usually faster than hand-written code. They also work for any list length.
4. A sorted list of 1,000,000 items needs to be searched. Which is the better algorithm?
A. Binary search ✓
B. Linear search
Binary search halves the search area each step, so it needs far fewer checks. Choosing a better algorithm is one of the biggest optimisations.
5. Which statement about optimisation is true?
A. Optimised code must still give the same output as the original ✓
B. Optimised code is allowed to give different output if it runs faster
Always test after optimising. The same test cases should still pass.

Match the Pairs

total = sum(marks) → Uses a built-in function to add up a list, replacing a longer hand-written loop
def show(x): print(x * 2) → Moves repeated code into one function that can be called many times
Choosing a better algorithm: binary search on a sorted list is faster than checking every item → mid = (low + high) // 2
Readability vs speed: a clear variable name makes code easier to maintain even if it is not faster → average_mark = total / count
for i in range(len(names)): print(len(names)) → len(names) is recalculated on every pass of the loop instead of being stored once in a variable
print(a * 2) print(b * 2) print(c * 2) → The same calculation is copied three times instead of using a loop or function to remove the repeated code

Key Term

optimisation
Changing code so it runs faster, uses less memory or is easier to read, without changing what it does. For example, removing repeated code or using a built-in function.

Fill in the Blanks

1. total = sum(marks)
Hint: Type the built-in function that adds up every item in a list, instead of writing your own loop.
2. for i in range(5): print("Hello")
Hint: Type the built-in function that generates a sequence of numbers, so one loop replaces five repeated print lines.
3. mid = (low + high) // 2
Hint: Type the operator that divides and gives a whole number, used to find the middle in a faster binary search.
4. ordered = sorted(scores)
Hint: Type the built-in function that returns a new sorted list, rather than writing your own sorting code.
5. limit = len(items) while i < limit:
Hint: Type the variable that stores the length, so it is worked out once and not on every loop.

Spot the Error

Question 1
A marks = [72, 88, 65, 91]
B total = sum(marks)
C average = total / len(total)
D print(average)
Line C — len() needs a list, but total is a single number, so this causes a TypeError. Using the built-in sum() is a good optimisation, but the average must divide by len(marks).
Question 2
A names = ['Ava', 'Liam']
B for name in len(names):
C print('Hi', name)
Line B — len(names) is a number and cannot be looped over, so this causes a TypeError. A loop replaces repeated print lines, but it must loop over the list itself: for name in names:
Question 3
A def total(items)
B return sum(items)
C print(total([4, 8, 15]))
Line A — A function header must end with a colon. Putting the repeated work in one function removes duplicated code, but the header must be def total(items):
Question 4
A nums = [3, 9, 4, 7]
B low = min(nums)
C high = max(nums)
D print(high - lows)
Line D — The variable lows was never created, so this causes a NameError. The built-in min() and max() store the values in low and high, so the line should be print(high - low).
Question 5
A define find(items, x):
B if x in items:
C return items.index(x)
D return -1
Line A — Python uses the keyword def, not define, to create a function. Using built-in in and .index() is shorter than writing your own loop, but the header must be def find(items, x):

Student with a class code? Join your class →