Timing it

Sometimes you may need to measure the performance of the code or at least want to know which piece of the code is slowing down. Python has a built-in timing module to do this.


In [1]:
import timeit

Lets use time it to time various methods of creating the string '0-1-2-3-.....-99'

We'll pass two arguments the actual line we want to test encapsulated as a string and the number of times we wish to run it. Here we'll choose 10,000 runs to get some high enough numbers to compare various methods.


In [2]:
# Using For Loop
timeit.timeit('"-".join(str(n) for n in range(100))', number=10000)


Out[2]:
0.3370125970104709

In [3]:
# Using List Comprehension
timeit.timeit('"-".join([str(n) for n in range(100)])', number=10000)


Out[3]:
0.29157113702967763

In [4]:
# Using Map Function
timeit.timeit('"-".join(map(str, range(100)))', number=10000)


Out[4]:
0.24109973199665546

It also has a Command-Line Interface


In [ ]:
# $ python3 -m timeit '"-".join(str(n) for n in range(100))'
# 10000 loops, best of 3: 30.2 usec per loop
# $ python3 -m timeit '"-".join([str(n) for n in range(100)])'
# 10000 loops, best of 3: 27.5 usec per loop
# $ python3 -m timeit '"-".join(map(str, range(100)))'
# 10000 loops, best of 3: 23.2 usec per loop

Now lets introduce iPython's magic function %timeit

iPython's %timeit will perform the code in the same line a certain number of times (loops) and will give you the fastest performance time (best of 3).

Lets repeat the above examinations using iPython magic!


In [5]:
%timeit "-".join(str(n) for n in range(100))


10000 loops, best of 3: 31.9 µs per loop

In [6]:
%timeit "-".join([str(n) for n in range(100)])


10000 loops, best of 3: 27.1 µs per loop

In [7]:
%timeit "-".join(map(str, range(100)))


10000 loops, best of 3: 21.5 µs per loop

Date & Time

The datetime module supplies classes for manipulating dates and times in both simple and complex ways.

date


In [9]:
from datetime import date
now = date.today()
print(now)


2017-10-29

In [15]:
now.strftime("%m-%d-%y. %d %b %Y is a %A on the %d day of %B.")


Out[15]:
'10-29-17. 29 Oct 2017 is a Sunday on the 29 day of October.'

In [16]:
print('ctime:', now.ctime())
print('tuple:', now.timetuple())
print('ordinal:', now.toordinal())
print('Year:', now.year)
print('Mon :', now.month)
print('Day :', now.day)


ctime: Sun Oct 29 00:00:00 2017
tuple: time.struct_time(tm_year=2017, tm_mon=10, tm_mday=29, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=302, tm_isdst=-1)
ordinal: 736631
Year: 2017
Mon : 10
Day : 29

In [18]:
print('Earliest  :', date.min)
print('Latest    :', date.max)
print('Resolution:', date.resolution)


Earliest  : 0001-01-01
Latest    : 9999-12-31
Resolution: 1 day, 0:00:00

In [21]:
d1 = date(2017, 3, 11)
print('d1:', d1)

d2 = d1.replace(year=1997)
print('d2:', d2)


d1: 2017-03-11
d2: 1997-03-11

time


In [30]:
from datetime import  time
t = time(5,20,25,100)
print(t)


05:20:25.000100

In [31]:
print('hour  :', t.hour)
print('minute:', t.minute)
print('second:', t.second)
print('microsecond:', t.microsecond)
print('tzinfo:', t.tzinfo)


hour  : 5
minute: 20
second: 25
microsecond: 100
tzinfo: None

In [33]:
print('Earliest  :', time.min)
print('Latest    :', time.max)
print('Resolution:', time.resolution)


Earliest  : 00:00:00
Latest    : 23:59:59.999999
Resolution: 0:00:00.000001

arithmetic


In [14]:
birthday = date(1971,4,22)
age = now - birthday
age.days


Out[14]:
16992

In [22]:
d1 - d2


Out[22]:
datetime.timedelta(7305)