I've been testing some optimisations to a piece of code (specifically, whether elif n in [2,3] is faster than elif n == 2 or n == 3) and noticed something strange. 
Using timeit.default_timer I did several runs of each version of the function, and the first run was always significantly slower than subsequent ones (values starting off around 0.01 of a second that trailed off to consistently around 0.003). 
Is python doing something behind the scenes to optimise the code on later runs? This isn't really a problem by any means, but I'd be interested to know what is happening (if anything)
Yes, python caches the pyc file after the first run, so if the code doesn't change it will run faster in next iterations because it won't need to compile it to byte code again. Remember python is an interpreted language, it's just skipping an interpretation step.
Why is the first run of timeit usually slower than subsequent ones?