Learn · CERN

Talk28 min

Writing faster Python

Sebastian Witowski · CERN

Watch At CERN

About

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

The talk

  1. Writing faster Python 2016-05-31 · 28 min · CERN's player

Credits and licence

Institution
CERN
Department
Developers@CERN Forum
Speaker
Sebastian Witowski
Year
2016
Language
English
Subjects
Literature and writing
Source
CERN
Licence
CERN, as CERN states it. CERN's terms
Attribution
© CERN. CERN provides its audiovisual media free of charge for educational and informational use, credited to CERN; this lecture is shown in CERN's own player.
Who can watch
CERN gives no rating. Invisible Network shows it to Teens profiles and older, judged from what the source says about it. How courses are placed