Preliminary Software Engineering
Unit 1 - Programming Fundamentals
Unit 1 - Programming Fundamentals
  • 1 - Programming Fundamentals
    • Programming Fundamentals Content
  • 2 - Python
    • Expected Python Knowledge
    • GitHub
    • Learning Python
    • Data Structures and File Management
      • Data Structures
        • Lists
        • Arrays
          • Single and Multi-Dimensional Arrays
        • Lists vs Arrays
          • Activities
        • Tuples
        • Sets
        • Dictionaries
          • Activities
      • File Handling
        • Loops, Lists, Dictionaries
        • Activities
  • 3 - Theory Content
    • Theory Content Explained
      • NESA Directional Verbs
      • Responding to Directional Verbs
  • 4 - Software Development and Management
    • Approaches to Software Development
      • Waterfall Model
      • Agile Model
      • Summary: Waterfall vs Agile
      • Activities
    • Software Development Steps
      • Sample Exam Question
      • Requirements Definition
        • Understanding the Need
        • Key Questions to Ask
        • Examples
        • Activities
        • Sample Exam Question
      • Determining Specifications
        • Functional Specifications
        • Non-Functional Specifications
        • Activities
      • Design
        • Top-Down Design Approach
        • Bottom-Up Design Approach
      • Development
        • Optimising Code
      • Integration
        • Example: Payment Program
        • Activity
        • Application Programming Interface (API)
          • Example: OpenWeather API
          • Example: WeatherAPI
          • Activity: Prepare Spells
      • Testing and Debugging
        • Test Data
          • Activities
        • Testing the System
          • Activities
        • Debugging
          • Types of Errors
            • Activities
          • Python Debugger
            • Activities
          • VS Code Debugger
            • Activities
      • Installation
        • Activities
        • Sample Exam Question
      • Maintenance
  • Charts and Algorithms
    • Example: IPO Charts and Pseudocode
      • Activities
    • Algorithms, Flowcharts, Pseudocode
      • Pseudocode Activities
      • Flowchart Activities
      • Sample Exam Questions
    • Structure Charts
      • Activities
      • Sample Exam Questions
    • Data Flow Diagrams
      • Activities
    • Data Dictionaries
      • Activities
    • Decision Trees
      • Activities
      • Sample Exam Questions
    • Gantt Chart
    • Class Diagrams
      • Sample Exam Question
    • Storyboards
      • Sample Exam Question
  • Testing and Debugging
    • Test Data
      • Activities
    • Testing the System
      • Activities
    • Debugging
      • Types of Errors
        • Activities
      • Python Debugger
        • Activities
      • VS Code Debugger
        • Activities
  • Computational Thinking
    • Decomposition
    • Abstraction
    • Activities
  • Version Control
    • Git
    • GitHub
    • Activities
  • Number Systems
    • Binary Systems
    • Hexadecimal Numbers
    • Using Two's Complement
    • Activities
  • 5 - Assessment Task 1
    • Data Science Project
      • Before we Start
        • Setting up GitHub Repository
        • Setting Up Markdown Documentation
      • Examples of API Usage
        • Starter Code: NASA Scenario
        • Starter Code: Spell Book
        • Starter: Pokédex Explorer
        • Starter Code: Weather App
        • Example: OpenWeather API
        • Example: WeatherAPI
        • Example: Prepare Spells
    • Task Guide
      • Requirements Definition
      • Determining Specifications
        • Use Cases
      • Design
        • Gantt Chart
        • Structure Chart
        • Algorithms
        • Data Dictionary
      • Development
        • Comments vs DocStrings
        • UI - main.py
        • Create Python Module
          • Example: NASA Module
          • Example: WeatherFetch Module
          • Example: SpellBook Module
      • Integration
        • Example: Pokedex
      • Testing and Debugging
        • Commit Changes
      • Installation
      • Maintenance
    • Submitting Your Task
Powered by GitBook
On this page
  1. 4 - Software Development and Management
  2. Software Development Steps
  3. Development

Optimising Code

PreviousDevelopmentNextIntegration

Last updated 3 months ago

It is important that when creating our code we are as efficient as possible. This not only saves time, but also saves storage and improves efficiency when running a program.

Look at the following for advice on how we can best optimise code - you may even find you come across functions you have not used before!

Code Practice
Unoptimised
Optimised

Using List Comprehensions

Instead of using loops to create or transform lists, use list comprehensions, which are more concise and often faster.

Using Generator Expressions for Large Data

Generators are more memory-efficient than lists when dealing with large datasets because they generate items on the fly rather than storing them all in memory.

Using Built-In Functions

Python's built-in functions are implemented in C and are highly optimised for performance.

Avoiding Unnecessary List Copying

When you slice a list, Python creates a new list object. To avoid unnecessary copying, work with the original list when possible.

Avoiding Global Variables

Accessing global variables is slower than accessing local variables. It is often better to pass variables as arguments to functions instead of relying on global state.

Using join() for String Concatenation

Concatenating strings with + can be inefficient because it creates a new string object each time. Instead, use join() to concatenate a list of strings.

Using enumerate() for Indexed Iteration

This technique avoids manual index management.

Minimising Function Calls in Loops

Repeated function calls in loops can slow down performance. Instead, store function results in a variable if they are called multiple times.

Using any() and all()

These functions are useful for checking conditions in a collection and are faster than manually looping through each element.

squares = [] 
for i in range(10): 
    squares.append(i * i)
squares = [i * i for i in range(10)]
squares = [i * i for i in range(1000000)]
squares = (i * i for i in range(1000000))
total = 0 
for num in range(1, 1001): 
    total += num
total = sum(range(1, 1001))
new_list = my_list[:]
new_list = my_list
x = 10 
def add_to_x(y): 
    return x + y
def add_to_x(x, y): 
    return x + y
words = ["Hello", "world", "from", "Python"]
sentence = ""
for word in words:
    sentence += word + " "
words = ["Hello", "world", "from", "Python"]
sentence = " ".join(words)
i = 0
for item in my_list:
    print(i, item)
    i += 1
for i, item in enumerate(my_list):
    print(i, item)
for i in range(len(my_list)):
    print(len(my_list))
list_length = len(my_list)
for i in range(list_length):
    print(list_length)
found = False
for item in my_list:
    if item > 10:
        found = True
        break
found = any(item > 10 for item in my_list)