GCSE · Computer Science · AQA · Spec 8525
Algorithm efficiency
Two programs print exactly the same answer. One of them did ten times the work to get there. Can you tell which, without a stopwatch?
Watch the work pile up
Two ways to add up 1 to 10
Both methods get the right answer. The question is how much work each one does to get there. count A and count B tally the calculations each method carries out: every +, × or ÷ counts as one.
Before you press Play: which method do you think will do more work?
1 // Method A: add the numbers one at a time2 total ← 03 FOR i ← 1 TO n4 total ← total + i5 ENDFOR6 OUTPUT total7 // Method B: use a formula8 answer ← n * (n + 1) / 29 OUTPUT answer
Variables
Output
Step 1: Same job for both methods: add up 1 + 2 + 3 + … + 10. Method A starts its running total at 0. Nothing has been added yet, so count A is 0.
Press Next, or Play, and keep your eye on count A and count B.
Predict, then check
Back to Methods A and B from the top of the page.
Try n = 3. Method A does 3 additions (1 + 2 + 3), and Method B does 3 calculations. It's a dead heat! Now make n = 1,000. What happens to the two counts?
When the data changes the work
Best case, worst case, or in between?
How many characters does the algorithm have to check for each password? Sort each one.
Still to sort
Best case (0)
The fewest checks possible: just 1.
Where the line is: Best case means the work stops as early as it possibly can, at the very first character.
Worst case (0)
The most checks possible: all 6.
Where the line is: Worst case isn't only 'no digit'. Any password that forces all 6 checks is a worst case.
In between (0)
More than 1 check but fewer than 6.
Where the line is: These stop part-way through: more work than the best case, less than the worst.
A new job. An algorithm checks whether a 6-character password contains a digit. It looks at the characters one at a time from the left, and stops as soon as it finds a digit. If it reaches the end without finding one, the answer is no. Method A always did one addition per number; this algorithm's work depends on the password itself.
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
Two algorithms can both get the right answer while one of them does far more work. Efficiency is about counting that work.
What you need to know
- A problem can usually be solved by several algorithms. All can be correct, yet need very different amounts of work.
- Efficiency means how quickly an algorithm completes its task, judged by counting its basic steps, such as comparisons or loop repetitions.
- Count steps, not seconds. The count doesn't depend on the hardware, so the comparison is fair.
- To compare two algorithms, count the steps each one carries out on the same input.
Have a goBen announces: 'My count is 8 steps and Ana's is 80, so I win!' Ben's list had 8 names. Ana's had 80. Why is Ben getting ahead of himself?
Their counts come from different inputs, so they can't be compared yet.
A fair comparison counts both algorithms on the same input; a smaller list can give a smaller count whichever algorithm is used.
- Then see how each count grows as the input gets bigger. The count that grows more slowly belongs to the more efficient algorithm.
Have a goAlgorithm S takes 10 steps on 10 items and 20 steps on 20 items. Algorithm T takes 10 steps on 10 items and 400 steps on 20 items. Which count is growing more slowly?
S's count: it goes from 10 to 20, while T's jumps from 10 to 400.
They start level, so the starting point can't separate them. What matters is what each count does as the input gets bigger.
- The more efficient algorithm does less to reach the same result, for example checking fewer items or repeating a loop fewer times.
- The number of steps can depend on the data. Best case is the fewest steps possible; worst case is the most steps possible.
Have a goYour lost sock is somewhere in a drawer of 9 socks. You check them one at a time from the top and stop when you find it. How many socks do you check in the best case, and in the worst case?
Best case 1 sock (it's on top). Worst case 9 socks (it's at the very bottom).
Best and worst case depend on where the work stops: the fewest checks possible and the most checks possible.
- Efficiency differences matter most with large amounts of data. On a small input, two algorithms may perform similarly.
The big picture
Most problems can be solved by more than one algorithm, and every one of them can be correct while needing very different amounts of work. We judge efficiency by counting basic steps, like comparisons or loop repetitions, not by timing a program on one particular computer. To compare two algorithms, count the steps each does on the same input, then see how those counts grow as the input gets bigger: the count that grows more slowly belongs to the more efficient algorithm. The data itself can change the step count, which is why we talk about best and worst cases.
Key points
Worked example
Problem
Two algorithms decide whether a whole number n is even. Algorithm X keeps subtracting 2 from n until what's left is 0 or 1; if it's 0, n is even. Algorithm Y works out n MOD 2 once; if the result is 0, n is even. For n = 12, count the steps each algorithm needs. Which is more efficient, and why?
⚠ Watch out
Comparing two algorithms on different inputs, for example one on a list of 5 items and the other on a list of 500. The step counts only mean something when both algorithms get the same input.
Memory hook
Don't time it, count it. Then make the input bigger and count again.
Check yourself
Cover the page. What does efficiency measure, and why not just time it? How do you compare two algorithms fairly? What are best and worst cases? Why does big data matter?
Flashcards
(10)Can two different algorithms both give the correct result for the same problem?
What does an algorithm's efficiency mean in this topic?
Why count steps instead of timing a program?
Give two examples of basic steps you could count.
What must be the same when you compare two algorithms' step counts?
Judged by growth, which of two algorithms is more efficient?
Where does the more efficient algorithm's advantage come from?
What's the difference between an algorithm's best case and its worst case?
When do differences in efficiency matter most?
Does a shorter program always do less work?
Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.
Learning with Lightbulb is opening soon
You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.
Keep me postedMore AQA GCSE Computer Science topics
How this lesson was checked. This AQA GCSE Computer Science (specification 8525)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 23 October 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.