# Writing faster Python

- Institution: CERN
- Department: Developers@CERN Forum
- Speaker: Sebastian Witowski
- Year: 2016
- Language: English
- Subjects: Literature and writing
- Source: [CERN](https://videos.cern.ch/record/3016879)
- Licence: [CERN](http://copyright.web.cern.ch)
- Who can watch: Teens profiles and older (sources give no ratings; placed by Invisible Network from the source's own words)

Did you know that Python preallocates integers from -5 to 257 ? Reusing them 1000 times, instead of allocating memory for a bigger integer, can save you a couple of milliseconds of code's execution time. If you want to learn more about this kind of optimizations then, … well, probably this presentation is not for you :) Instead of going into such small details, I will talk about more "sane" ideas for writing faster code. After a very brief overview of how to optimize Python code (rule 1: don't do this; rule 2: don't do this yet; rule 3: ok, but what if I really want to do this ?), I will show simple and fast ways of measuring the execution time and finally, discuss examples of how some code structures could be improved. You will see:

- What is the fastest way of removing duplicates from a list - How much faster your code is when you reuse the built-in functions instead of trying to reinvent the wheel - What is faster than the good ol' for loop - If the lookup is faster in a list or a set (and when it makes sense to use each) - How the "It's better to beg for forgiveness than to ask for permission" rule works in practice

I will NOT go into details of "serious" optimization, like using different Python implementation or rewriting critical code in C, etc..

0:00:00 Slide 1

0:00:09 Slide 2

0:00:33 Slide 3

0:01:03 Slide 4

0:01:19 Slide 5

0:01:35 Slide 6

0:01:51 Slide 7

0:02:05 Slide 8

0:02:28 Slide 9

0:02:45 Slide 10

0:02:53 Slide 11

0:02:59 Slide 12

0:03:05 Slide 13

0:04:07 Slide 14

0:04:13 Slide 15

0:05:05 Slide 16

0:05:33 Slide 17

0:06:02 Slide 18

0:06:45 Slide 19

0:07:00 Slide 20

0:07:16 Slide 21

0:07:44 Slide 22

0:07:58 Slide 23

0:08:16 Slide 24

0:08:57 Slide 25

0:09:04 Slide 26

0:09:48 Slide 27

0:10:10 Slide 28

0:10:18 Slide 29

0:10:54 Slide 30

0:11:23 Slide 31

0:11:49 Slide 32

0:12:09 Slide 33

0:12:54 Slide 34

0:13:04 Slide 35

0:13:45 Slide 36

0:14:35 Slide 37

0:15:02 Slide 38

0:15:54 Slide 39

0:16:12 Slide 40

0:16:24…

## Lectures

1. [Writing faster Python](https://invisiblenetwork.ai/learn/cern/3016879/1149360ad9ac), 28 min (CERN's player)
