In [2]:
from tqdm import tqdm
from time import sleep
In [5]:
for i in tqdm(range(10)):
sleep(0.02)
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)
In [10]:
a = g(3)
print(a)
print(g(3))
b = h(a)
b2 = h(a)
print(b2)
np.allclose(a,b, b2)
Out[10]:
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')
In [13]:
def fib(n):
if n<2:
return n
return fib(n-1)+fib(n-2)
In [14]:
%timeit fib(20)
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)
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)
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)
In [10]:
%timeit fib_cache(100)