GCSE · Computer Science · Eduqas · Spec C500QS
Code optimisation (compilation stage)
What if two programs gave exactly the same answers every single time, but one did the job with less work? That is what the compiler quietly makes happen for you.
Computer Science · Compilation
Two programs, one answer: you call it
Pick a tab, step through it, and call each marked step before the table shows it. Then switch tabs and compare.
Both versions of the program are listed. Each tab runs one version on one input. Call the marked steps; nothing is run, every value comes from the lesson.
// ORIGINAL PROGRAMhours = INPUTseconds = hours * (60 * 60)if 2 > 5 thenprint("Checking")endifprint(seconds)// OPTIMISED PROGRAMhours = INPUTseconds = hours * 3600print(seconds)
| hours | seconds |
|---|---|
| 2 |
Step 1. The user types 2, so hours holds 2. This first step is worked for you.
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
How a compiler makes your program do less work without changing a single answer
What you need to know
- A compiler translates a high-level language program, and code optimisation is one of the stages it carries out along the way.
- At that stage the compiler improves the code so the program runs more efficiently.
- Optimisation never changes what the program does. The same inputs give the same results as before.
Have a goSam says: "The compiler optimised my program, so now it should print a better answer." Given the same input, what will it print?
Exactly the same answer as before.
Optimisation changes the code, never the results from the same inputs.
- Running more efficiently means executing faster (fewer or quicker instructions), using less memory, or both.
Have a goAn optimised program carries out fewer instructions than the original, and its results are unchanged. Which efficiency gain does that show?
It runs faster, because fewer instructions are carried out.
Fewer instructions is one way to run faster. Using less memory is the other, and nothing here says memory changed.
- The compiler gets there by removing or simplifying unnecessary instructions.
- Example one: code that can never be executed gets removed.
- Example two: a calculation whose result is already known gets replaced by that result.
Have a goTwo lines from a program: y = 8 * 4, and an if 5 > 9 then ... endif block. What could the compiler do to each?
Replace 8 * 4 with 32; remove the whole if block.
8 * 4 is a calculation whose result is already known, and 5 > 9 is false every time, so its code can never be executed.
- So the optimised program is different code that still gives exactly the same results.
The big picture
Code optimisation is a stage of compilation in which the compiler improves the code so the program runs more efficiently: faster and/or using less memory. It does this by removing or simplifying unnecessary instructions, and the optimised program always gives the same results from the same inputs.
Key points
Worked example
Problem
The compiler's optimiser meets this program: cost = 5 * 20, then if 1 > 2 then print("Free") endif, then print(cost). Show the program after the code optimisation stage, and show that its output has not changed.
⚠ Watch out
Thinking optimisation changes or fixes what a program does, or that it is the programmer tidying their code. It is the compiler's stage, and the optimised program gives the same results from the same inputs.
Memory hook
Different code. Same answers. Less work.
Check yourself
A program asks the user for a number n and prints n * 2. Can the compiler replace n * 2 with a fixed result such as 10? Say why or why not.
Flashcards
(8)What is code optimisation?
Who carries out code optimisation, and when?
Does code optimisation change what a program does?
What does 'runs more efficiently' mean for optimised code?
How does the compiler make code more efficient?
Optimisation example: what happens to code that can never be executed?
Optimisation example: what happens to a calculation whose result is already known?
True or false: optimisation finds and fixes errors in your program.
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 Eduqas GCSE Computer Science topics
How this lesson was checked. This Eduqas GCSE Computer Science (specification C500QS)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 16 November 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.