In [1]:
import math
import random
import numpy as np
from numba import jit, vectorize, float64
import matplotlib.pyplot as plt
import seaborn
%matplotlib inline
In [2]:
def step():
return 1. if random.random() > .5 else -1.
In [3]:
def walk(n):
x = np.zeros(n)
dx = 1. / n
for i in range(n - 1):
x_new = x[i] + dx * step()
if x_new > 5e-3:
x[i + 1] = 0.
else:
x[i + 1] = x_new
return x
In [4]:
n = 100000
x = walk(n)
In [5]:
plt.plot(x)
In [6]:
%%timeit
walk(n)
Out[6]:
In [7]:
@jit(nopython=True)
def step_numba():
return 1. if random.random() > .5 else -1.
In [8]:
@jit(nopython=True)
def walk_numba(n):
x = np.zeros(n)
dx = 1. / n
for i in range(n - 1):
x_new = x[i] + dx * step_numba()
if x_new > 5e-3:
x[i + 1] = 0.
else:
x[i + 1] = x_new
return x
In [9]:
%%timeit
walk_numba(n)
Out[9]:
In [10]:
x = np.random.rand(10000000)
%timeit np.cos(2*x**2 + 3*x + 4*np.exp(x**3))
Out[10]:
In [11]:
@vectorize
def kernel(x):
return np.cos(2*x**2 + 3*x + 4*np.exp(x**3))
In [12]:
kernel(1.)
Out[12]:
In [13]:
%timeit kernel(x)
Out[13]:
In [14]:
import numexpr
%timeit numexpr.evaluate('cos(2*x**2 + 3*x + 4*exp(x**3))')
Out[14]:
In [15]:
numexpr.detect_number_of_cores()
Out[15]: