---
# System prepended metadata

title: COMP0233 - Day 10 (15th December 2022)
tags: [COMP0233-22-23, teaching, class]

---

# COMP0233 - Day 10 (15th December 2022)

:::info
## :tada: Welcome to the tenth day! Speed :clock1: 
 

### Today
Today we will look at 
- measuring how long code takes to run
- accelerating Python code
- where the time is spent
:::


:::warning 
| Room | groups |
|---   | ---    |
| 1.05 | 7, 8, 10, 12, 13, 15, 18 |
| 1.21 | 1, 2, 3, 4, 5, 6, 9      |
| 4.06 | 11, 14, 16, 19, 20, 21   |

:::

:::danger
**Only use zoom if you are connecting remotely or need subtitles**
Remember to `rename` yourself by clicking on your zoom name (in the participants list) and change it to:
`SXXX` where XXX is the room number (105, 121 or 406 or 000 for the online attendees).

If you disconnect and connect again, please rename yourself again.

**Use Youtube If you are in class***
⏯ [streaming - youtube](https://youtube.com/live/sTHPDk13Oe8?feature=share) **UPDATED**
:::


### Review from last week 

- improving readability of code
- refactoring 

#### What was discussed during the week :newspaper:
- assignment released (quick summary at the end of the class)
    - ipac trial
- Homework questionnaire
  
### Understanding quiz

Head to [Mentimeter](https://www.menti.com/alkj1kc2hrwg) (or code 6710 3141).

### :handshake:  Groups Discussion
:::warning
:question: **What makes code slow? How can we speed up code?**

- Write Notes in the class pad for your group.
- Nominate or volunteer one person for each group to feedback.
:::

- **Group 1**
   - What makes code slow?
      -.
      -.
    - How can we speed up code?
      -.
      -.

- **Group 2**
   - What makes code slow?
      -.
      -.
    - How can we speed up code?
      -.
      -.

- **Group 3**
   - What makes code slow?
      -. the  structures and algorithms used
      -. computer memory
      -. nested the loop
    - How can we speed up code?
      -. optimize the coding
      -. better computer
      -. cuda in Numba to compile code
- **Group 4**
   - What makes code slow?
      -.multiple for loops
      -.large dataset 
    - How can we speed up code?
      -.pre-allocating variables
      -.

- **Group 5**
   - What makes code slow?
      -.huge dataset to read
      -.mutiple nested loops
    - How can we speed up code?
      -.optimize the structure
      -.reduce time complexity

- **Group 6**
   - What makes code slow?
      -.
      -.
    - How can we speed up code?
      -.
      -.

- **Group 7**
   - What makes code slow?
      -.complexity
      -.multiple for-loops (and nested)
      -.internal copies; growing vectors
    - How can we speed up code?
      -.parallisation
      -.code in a faster language :)
      -.pick an appropriate library 

- **Group 8**
   - What makes code slow?
      -.
      -.
    - How can we speed up code?
      -.
      -.

- **Group 9**
   - What makes code slow?
      -. Inefficient structure
      -. 
    - How can we speed up code?
      -. vectorisation
      -. 

- **Group 10**
   - What makes code slow?
      -.
      -.
    - How can we speed up code?
      -.
      -.

- **Group 11**
   - What makes code slow?
      -.
      -.
    - How can we speed up code?
      -.
      -.

- **Group 12**
   - What makes code slow?
      -. Use libraries that are not the most proper tools for our work now.
      -. Nested for loops.
    - How can we speed up code?
      -. Use more appropriate library.
      -. Try other languages.

- **Group 13**
   - What makes code slow?
      -.
      -.
    - How can we speed up code?
      -.
      -.

- **Group 14**
   - What makes code slow?
      - Unnecessary loops
      - Use of inefficient algorithms (eg. bubble sort)
      - Unnecessarily accessing same data multiple times
    - How can we speed up code?
      - SIMD Optimisation (single instruction, multiple data)
      - Use of a low-level language (eg. C, maybe C++) for performance-critical operations
      - Multiprocessing / Parallelisation / GPU acceleration

- **Group 15**
   - What makes code slow?
      -.   
      -.
    - How can we speed up code?
      -.optimize data structure
      -.

- **Group 16**
   - What makes code slow?
      - .
      -.
    - How can we speed up code?
      -.
      -.

- **Group 17**
   - What makes code slow?
      -.
      -.
    - How can we speed up code?
      -.
      -.

- **Group 18**
   - What makes code slow?
      - unnecessary computation in loops
      - excessive repeated looping
    - How can we speed up code?
      - use more appropriate mathematical equations/function for slower algorithms
      - store values computed many times in a variable
      - reordering independent if conditions to optimise code

- **Group 19**
   - What makes code slow?
      - complex construction 
      -.
    - How can we speed up code?
      - use a better hardware
      -.

- **Group 20**
   - What makes code slow?
      - re-writing the same method over and over
      - using nested loops without the neeed to 
      - using Inefficient algorithms 
      - 
    - How can we speed up code?
      - use the python libraries, as they are optimised 
      - use faster hardware, with faster GPU
      - use of JIT compiler
      -.

- **Group 21**
   - What makes code slow?
      - Repeated loops
      - Bad memory allocation/memory leaks
    - How can we speed up code?
      - Vectorization
      - Refactoring to remove unneccesary processing
###  Exercise: Accelerating code :fast_forward: 
:::info

In the homework, you looked at using numpy to accelerate Python code. For this exercise, we will look at two more ways.

Based on your group number, look at the following issue:
| Group number | Issue |
|- | - | 
|Even (2, 4, ...) |[:hash::five::six: Numba and Cython](https://github.com/UCL-COMP0233-22-23/RSE-Classwork/issues/56)|
|Odd (1, 3, ...) |[:hash::five::seven: MPI](https://github.com/UCL-COMP0233-22-23/RSE-Classwork/issues/57) |


Follow the instructions to look at the code, run it and measure its performance.

Write down your comments in the space below. Focus on:
- Was the code easy to understand?
- How different was it compared to the original? How easy do you think it is to adapt the original code to this form?
- Does it perform better than the original? Than the `numpy` version? What conditions do you think would make it run faster/slower?
Nominate one person from your group to explain the method and feed back to the class.
:::

- **Group 1**
    -.
    -.
    -.

- **Group 2**
    -.
    -.
    -.

- **Group 3**
    - More difficult to understand than original code.
    - Faster than the basic implementation, not faster than numpy (10 times slower) 
    - Numpy: pi = 3.1444 (with 10000)
        100 loops, best of 5: 369 μsec per loop
    - Original: pi = 3.1488 (with 10000)
        100 loops, best of 5: 8.58 msec per loop
    - MPI: pi = 3.1368 (with 10000 points)
        It took: 5.52 msec
    - The version is quite different from the original one. The code runs in different nodes, processors and it is a bit more challenging to debug if something goes wrong.
    - To make it faster, maybe we can use some numpy tools to make the code more efficient.

- **Group 4**
    -.
    -.
    -.

- **Group 5**
    -.
    -.
    -.

- **Group 6**
    -.
    -.
    -.

- **Group 7**
    - The MCMC does not estimate pi very well... 
    - MPI is faster than the Python implementation
    - Numpy is half as quick as MPI 

- **Group 8**
    -.
    -.
    -.

- **Group 9**
    -.
    -.
    -.

- **Group 10**
    -. Numba is very similar to the original. Very little difference in terms of readability.
    -.

- **Group 11**
    -.
    -.
    -.

- **Group 12**
    -.There are not too many differences between Numba and original code.
    -.
    -.

- **Group 13**
    -.
    -.
    -.

- **Group 14**
    - Numba and Cython's code are almost identical and easy to read. The code is similar to the original version, but slightly different to the numpy version.
    - Speed:
        - Original: 12.3 ms per loop
        - Numpy: 311 μs per loop
        - Numba: 1.59 s (with compilation), 0 ns (after)
        - Cython: 13.6 ms
    - Numba speeds up the code using JIT (just-in-time compilation). It has to perform it for the first loop, so this will be slower. But afterwards, it speeds up.
    - Cython converts the python code to C. As the code isn't type annotated, it might be slower as Cython has to guess its type.

- **Group 15**
    -.
    -.
    -.

- **Group 16**
    - We found Cython quite readable. Numba as well, and Numba was the fastest.
    - Numba:
        Elapsed (with compilation) = 1.66 sec
        pi = 3.1436 (with 10000)
        Elapsed (after compilation) = 199 μsec
        pi = 3.164 (with 10000)
    -.
    -.

- **Group 17**
    -.
    -.
    -.

- **Group 18**
    - Numba:
        - used 0 ns after the program had been compiled
        - uses @jit which causes code to be run at a lower level so no need to touch API
        - no use of objects
    -.

- **Group 19**
    -.
    -.
    -.

- **Group 20**
    - Using Numba we have got the following results
    Elapsed (with compilation) = 1.72 sec
    pi = 3.1496 (with 10000)
    Elapsed (after compilation) = 138 μsec
    pi = 3.1244 (with 10000)

- **Group 21**
    - Could not get numba to install
    - It looked like @jit tags were being used to speed up the code

### Quiz: Scaling
Head back to  [Mentimeter](https://www.menti.com/alkj1kc2hrwg) (or code 6710 3141).


:film_frames: A video about performance
[Someone improved my code by 40,832,277,770%](https://www.youtube.com/watch?v=c33AZBnRHks)


### Homework 
- Assignment / IPAC
- :christmas_tree:
- :santa:
- :sleeping_accommodation:

# Questions

:::info
Feel free to add any question below. Remember the syntax:
```
- Example question
  - [name=student_a] Example answer
  - [name=TA_1] Example answer
```
:::

- ...

- ...



###### tags: `COMP0233-22-23` `teaching` `class`