Python

Timing of code snippets

on the CLI

python -m timeit -- "for i in range(1000): i**2"

in ipython

u = None
%timeit u is None

in a script

import timeit
from functools import partial


# func without parameters:
def stupid_test():
    L = [i for i in range(100)]


# func with parameters:
def another_test(x, y):
    return x ** y


print(timeit.timeit(stupid_test, number=1000))
print(timeit.timeit(partial(another_test, x=10, y=2), number=100000))

Useful interpreter options

python -W ignore  # ignore all warnings
python -O         # remove assert statements

A complete list of interpreter options can be found here.

See also