Examining runtime

Scrivere codice Python efficiente

Logan Thomas

Scientific Software Technical Trainer, Enthought

Why should we time our code?

  • Allows us to pick the optimal coding approach
  • Faster code == more efficient code!
Scrivere codice Python efficiente

How can we time our code?

  • Calculate runtime with IPython magic command %timeit

  • Magic commands: enhancements on top of normal Python syntax

    • Prefixed by the "%" character
    • Link to docs (here)
    • See all available magic commands with %lsmagic
Scrivere codice Python efficiente

Using %timeit

Code to be timed

import numpy as np

rand_nums = np.random.rand(1000)

Timing with %timeit

%timeit rand_nums = np.random.rand(1000)
8.61 µs ± 69.1 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
Scrivere codice Python efficiente

%timeit output

alt="Magic command timeit output"

Scrivere codice Python efficiente

%timeit output

alt="Magic command timeit output with mean value and standard deviation value highlighted"

Scrivere codice Python efficiente

%timeit output

alt="Magic command timeit output with number of runs and number of loops highlighted"

Scrivere codice Python efficiente

Specifying number of runs/loops

Setting the number of runs (-r) and/or loops (-n)

# Set number of runs to 2 (-r2)
# Set number of loops to 10 (-n10)

%timeit -r2 -n10 rand_nums = np.random.rand(1000)
16.9 µs ± 5.14 µs per loop (mean ± std. dev. of 2 runs, 10 loops each)
Scrivere codice Python efficiente

Using %timeit in line magic mode

Line magic (%timeit)

# Single line of code

%timeit nums = [x for x in range(10)]
914 ns ± 7.33 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
Scrivere codice Python efficiente

Using %timeit in cell magic mode

Cell magic (%%timeit)

# Multiple lines of code

%%timeit
nums = []
for x in range(10):
    nums.append(x)
1.17 µs ± 3.26 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
Scrivere codice Python efficiente

Saving output

Saving the output to a variable (-o)

times = %timeit -o rand_nums = np.random.rand(1000)
8.69 µs ± 91.4 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
Scrivere codice Python efficiente
times.timings
[8.697893059998023e-06,
 8.651204760008113e-06,
 8.634270530001232e-06,
 8.66847825998775e-06,
 8.619398139999247e-06,
 8.902550710008654e-06,
 8.633500570012985e-06]
times.best
8.619398139999247e-06
times.worst
8.902550710008654e-06
Scrivere codice Python efficiente

Comparing times

Python data structures can be created using formal name

formal_list = list()
formal_dict = dict()
formal_tuple = tuple()

Python data structures can be created using literal syntax

literal_list = []
literal_dict = {}
literal_tuple = ()
Scrivere codice Python efficiente
f_time = %timeit -o formal_dict = dict()
145 ns ± 1.5 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
l_time = %timeit -o literal_dict = {}
93.3 ns ± 1.88 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
diff = (f_time.average - l_time.average) * (10**9)
print('l_time better than f_time by {} ns'.format(diff))
l_time better than f_time by 51.90819192857814 ns
Scrivere codice Python efficiente

Comparing times

%timeit formal_dict = dict()
145 ns ± 1.5 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
%timeit literal_dict = {}
93.3 ns ± 1.88 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
Scrivere codice Python efficiente

Off to the races!

Scrivere codice Python efficiente

Preparing Video For Download...