In [1]:
%matplotlib inline
import xyzpy as xyz
import numpy as np
In [2]:
with xyz.Timer() as timer:
A = np.random.randn(512, 512)
el, ev = np.linalg.eig(A)
timer.interval
Out[2]:
In [3]:
def setup(n=512):
return np.random.randn(n, n)
def foo(A):
return np.linalg.eig(A)
xyz.benchmark(foo, setup=setup)
Out[3]:
Or we can specfic the size n to benchmark with as well:
In [4]:
xyz.benchmark(foo, setup=setup, n=1024)
Out[4]:
In [5]:
import numba as nb
def setup(n):
return np.random.randn(n)
def python_square_sum(xs):
y = 0.0
for x in xs:
y += x**2
return y**0.5
def numpy_square_sum(xs):
return (xs**2).sum()**0.5
@nb.njit
def numba_square_sum(xs):
y = 0.0
for x in xs:
y += x**2
return y**0.5
The setup function will be supplied to each, we can check they
first give the same answer:
In [6]:
xs = setup(100)
In [7]:
python_square_sum(xs)
Out[7]:
In [8]:
numpy_square_sum(xs)
Out[8]:
In [9]:
numba_square_sum(xs)
Out[9]:
In [10]:
kernels = [
python_square_sum,
numpy_square_sum,
numba_square_sum,
]
benchmarker = xyz.Benchmarker(
kernels,
setup=setup,
benchmark_opts={'min_t': 0.01}
)
Next we run a set of problem sizes:
In [11]:
sizes = [2**i for i in range(1, 11)]
benchmarker.run(sizes, verbosity=2)
Which we can then automatically plot:
In [12]:
benchmarker.ilineplot()
Out[12]:
In [13]:
import random
stats = xyz.RunningStatistics()
total = 0.0
# don't know how many `x` we'll generate, and won't keep them
while total < 100:
x = random.random()
total += x
stats.update(x)
We can now check a variety of information about the values generated:
In [14]:
print(" Count: {}".format(stats.count))
print(" Mean: {}".format(stats.mean))
print(" Variance: {}".format(stats.var))
print("Standard Deviation: {}".format(stats.std))
print(" Error on the mean: {}".format(stats.err))
print(" Relative Error: {}".format(stats.rel_err))
In [15]:
xs = [random.random() for _ in range(10000)]
In [16]:
stats.update_from_it(xs)
In [17]:
stats.count
Out[17]:
The relative error should now be much smaller:
In [18]:
stats.rel_err
Out[18]:
In [19]:
def rand_n_sum(n):
return np.random.rand(n).sum()
stats = xyz.estimate_from_repeats(rand_n_sum, n=1000, rtol=0.0001, verbosity=1)
We can then query the returned RunningStatistics object:
In [20]:
stats.mean
Out[20]:
In [21]:
stats.rel_err
Out[21]:
Which looks as expected.