Useful modules

Progress-bar


In [2]:
from tqdm import tqdm
from time import sleep

In [5]:
for i in tqdm(range(10)):
    sleep(0.02)



Caching


In [7]:
from tempfile import mkdtemp
cachedir = mkdtemp()
from joblib import Memory
memory = Memory(cachedir=cachedir, verbose=0)

In [8]:
import numpy as np

@memory.cache
def g(x):
    print('A long-running calculation, with parameter %s' % x)
    return np.hamming(x)

@memory.cache
def h(x):
    print('A second long-running calculation, using g(x)')
    return np.vander(x)

In [9]:
%timeit a = g(3)


A long-running calculation, with parameter 3
1000 loops, best of 3: 702 µs per loop

In [10]:
a = g(3)
print(a)
print(g(3))
b = h(a)
b2 = h(a)
print(b2)
np.allclose(a,b, b2)


[ 0.08  1.    0.08]
[ 0.08  1.    0.08]
A second long-running calculation, using g(x)
[[ 0.0064  0.08    1.    ]
 [ 1.      1.      1.    ]
 [ 0.0064  0.08    1.    ]]
Out[10]:
False

In [12]:
import folium
map_1 = folium.Map(location=[45.372, -121.6972])
map_1.simple_marker([45.3288, -121.6625], popup='Mt. Hood Meadows')
map_1.simple_marker([45.3311, -121.7113], popup='Timberline Lodge')
map_1.create_map(path='osm.html')

Cython speedup!


In [13]:
def fib(n):
    if n<2:
        return n
    return fib(n-1)+fib(n-2)

In [14]:
%timeit fib(20)


100 loops, best of 3: 2.97 ms per loop

In [15]:
%load_ext Cython

In [16]:
%%cython

def fib_cython(n):
    if n<2:
        return n
    return fib_cython(n-1)+fib_cython(n-2)

In [17]:
%timeit fib_cython(20)


1000 loops, best of 3: 1.08 ms per loop

In [18]:
%%cython

cpdef int fib_cython_type(int n):
    if n<2:
        return n
    return fib_cython_type(n-1)+fib_cython_type(n-2)

In [19]:
%timeit fib_cython_type(20)


10000 loops, best of 3: 43.9 µs per loop
Caching again

In [8]:
from functools32 import lru_cache as cache
# from functools import lru_cache as cache # in python 3

@cache(maxsize=None)
def fib_cache(n):
    if n<2:
        return n
    return fib_cache(n-1)+fib_cache(n-2)

In [9]:
%timeit fib_cache(20)


The slowest run took 287.11 times longer than the fastest. This could mean that an intermediate result is being cached 
1000000 loops, best of 3: 330 ns per loop

In [10]:
%timeit fib_cache(100)


The slowest run took 1421.86 times longer than the fastest. This could mean that an intermediate result is being cached 
1000000 loops, best of 3: 331 ns per loop