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 PROGRAM
hours = INPUT
seconds = hours * (60 * 60)
if 2 > 5 then
print("Checking")
endif
print(seconds)
// OPTIMISED PROGRAM
hours = INPUT
seconds = hours * 3600
print(seconds)
hoursseconds
2

Step 1. The user types 2, so hours holds 2. This first step is worked for you.

Step 1 of 4Called right: 0 of 2

Computer Science · Compilation

What do you think optimisation is?

Your teacher says: 'The compiler carries out code optimisation while it translates your program.' Four students each say what they think that means.

Which one is closest to what you think right now?
How sure are you?

Exam line: Optimisation is a compiler stage that improves the code so it runs more efficiently, and the results stay the same.

Computer Science · Compilation

Check the optimiser's work

A program reads a number from the user into score. Below are four changes an optimiser made to it. Three keep the results exactly the same. One would change the results for some inputs. Which line is the invalid change?

Changes made to the program — which line goes wrong?

Exam line: If a change could alter the results for any input, it is not a valid optimisation.

Computer Science · Compilation

Now say it in your own words

Describe what happens at the code optimisation stage when a high-level language program is compiled. [4 marks]

0 words · your answer stays on this page and is not sent anywhere.

Exam line: Four ideas, four marks: what the compiler does, an example, the efficiency gain, and the same results.

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

1Code optimisation is a compilation stage, so the compiler does it, not the programmer.
2Its purpose is efficiency: faster execution and/or less memory.
3It removes or simplifies unnecessary instructions: never-executed code goes, and a known calculation becomes its result.
4The results from the same inputs never change.

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?
One of the stages of compilation. The compiler improves the code so the program runs more efficiently.
Who carries out code optimisation, and when?
The compiler, as one stage while it translates a high-level language program.
Does code optimisation change what a program does?
No. The optimised program produces the same results from the same inputs as the unoptimised one.
What does 'runs more efficiently' mean for optimised code?
It executes faster (fewer or quicker instructions) and/or uses less memory.
How does the compiler make code more efficient?
By removing or simplifying unnecessary instructions.
Optimisation example: what happens to code that can never be executed?
The compiler removes it. It can never run, so taking it out changes no results.
Optimisation example: what happens to a calculation whose result is already known?
It is replaced with that result, so nothing needs working out when the program runs.
True or false: optimisation finds and fixes errors in your program.
False. It changes no results, so it does not correct what the program does. It only makes the same results cheaper to reach.

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 posted

More Eduqas GCSE Computer Science topics

See the full Eduqas Computer Science curriculum →

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.