Slides badge

Optimising your code

And why you should do it

Why do we optimise our code?

Code optimisation is one of the most vital aspects of high-performance computing.

 

We should never waste resources and aim to save where we can. Thinking of web technologies, imagine having to wait whilst the code is run on the server.

 

These slides will focus on the idea of code optimisation.

Compilation

Compilation is turning a string of code into either a bunch of machine code (1s and 0s) or into opcode.


Binary can be run natively on the machine. Opcode requires a special runtime that understands the opcode but can run it fast. 


C and C++ compile to binary whereas PHP, YASS and Python convert to opcode.


C and C++ compile code before it is run whereas PHP, YASS and Python compile the code each time it is run.

Areas of optimisation

  • Cache
  • Code order
  • Native functions
  • Early termination cases
  • Loops

Cache

With languages like Python, YASS and PHP, code is interpreted every time it is run. It is compiled to opcode. 


We can cache the opcode (YASS and PHP do this already) to save having to recompile the code each time.


But we can also cache in a different manner. If a PHP script reads and transforms the content of a file in to HTML for a webpage every time it is run, we can cache the transformation step so we just open the cached file.

Code order

When you write a program as below in Python:

...
if country == "spain":
  spainCode()
  exit()
elif country == "uk":
  ukCode()
  exit()
elif country == "germany":
  germanyCode()
  exit()
...

Imagine some 90% of people pick Germany. In order for the code to reach the code for Germany, it must first check if the country is "spain" or "uk" before finally getting to Germany.

The code can be made more effective by putting Germany as the first check, since we know most people are going for it. This becomes a huge improvement as the code gets more complex.

Code order

When you write a program as below in Python:

...
if country == "spain":
  spainCode()
  exit()
elif country == "uk":
  ukCode()
  exit()
elif country == "germany":
  germanyCode()
  exit()
...

Imagine some 90% of people pick Germany. In order for the code to reach the code for Germany, it must first check if the country is "spain" or "uk" before finally getting to Germany.

The code can be made more effective by putting Germany as the first check, since we know most people are going for it. This becomes a huge improvement as the code gets more complex.

Native functions

A native function is a function built into a language such as the pow function.

This program has a user-defined function called power and also uses the built-in pow function.


Both do the same thing in the end, but the pow function is already precompiled into binary, whereas the power function needs to be compiled before use.

def power(n):
  return n * n

print(pow(2))
print(power(2))

Early termination cases

Assume we have the following program that searches for an item in an array:

This program is inefficient! It finds an item but then completes the loop even when the item is found. 


If we had 100,000 items to search for, this will always loop 100,000 times, even if it finds the item immediately

def linearSearch(items, search):
  location = -1
  for i in range(0, len(items)):
    if items[i] == search:
      location = i
  return i

items = [2, 6, 9, 11, 22, 7, 34, 8]

This program fixes that by ending the second it finds the item it is looking for!

Early termination cases

Assume we have the following program that searches for an item in an array:

This program is inefficient! It finds an item but then completes the loop even when the item is found. 


If we had 100,000 items to search for, this will always loop 100,000 times, even if it finds the item immediately

def linearSearch(items, search):
  for i in range(0, len(items)):
    if items[i] == search:
      return i
  return -1

items = [2, 6, 9, 11, 22, 7, 34, 8]

This program fixes that by ending the second it finds the item it is looking for!

Loops

Not everyone knows everything about loops.


One area often ignored is that a loop checks it's condition every time it is run:

This program is inefficient! It finds an item but then completes the loop even when the item is found. 


If we had 100,000 items to search for, this will always loop 100,000 times, even if it finds the item immediately

items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for i in range(0, len(items)):
  print(items[i])

In this program, it checks the length of the array each time, despite the fact our array is static and the size is always 10. Function calls like the len function are costly in bigger programs.


This could be replaced by something less costly...

items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
length = len(items)
for i in range(0, length):
  print(items[i])

Loops

This now fixes that problem:

This program is inefficient! It finds an item but then completes the loop even when the item is found. 


If we had 100,000 items to search for, this will always loop 100,000 times, even if it finds the item immediately

items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for i in range(0, len(items)):
  print(items[i])

By using a variable we are performing fewer steps to do the same thing. Variable usage is much less demanding than a function call.


This is true in PHP, Python and YASS.

items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
length = len(items)
for i in range(0, length):
  print(items[i])

Optimisation is always necessary

For large scale programs, optimisation is always necessary.


Even if you gain just a 1ms improvement, that can add up later on.

Presentation Overview
Close
JB
Code Optimisation
© 2020 - 2024 J Balfour
19:20 | 23-05-2024
Join Live Session
Start Remote
Save Progress
Slideshow Outline
Presenter Mode
White Screen
Canvas Controls
Random Selector
Timer
Volume Meter
Binary Converter
Python Editor
Provide Feedback
Help
!
Keywords
    DragonDocs Management
    Random selector
    Sections
      Binary conversion
      Denary to binary conversion
      Binary to denary conversion
      Feedback 👍
      Accessibility

      Apply a filter:

      All slideshow files