Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
91 lines (68 sloc) 2.14 KB
"""
Benchmarks for argument dispatching and call overhead of ``@jit`` functions.
"""
import numpy as np
rec_dtype = np.dtype([('a', np.float64),
('b', np.int32),
('c', np.complex64),
])
samples = {
'bool': True,
'int': 100000,
'float': 0.5,
'complex': 0.5 + 1.0j,
'array_1d': np.zeros(10, dtype=np.int64),
'array_3d': np.zeros(20, dtype=np.float64).reshape(2, 2, 5),
'array_records': np.zeros(10, dtype=rec_dtype),
'recarray': np.recarray(10, dtype=rec_dtype),
'tuple': (0.5, 1.0j, ()),
'record': np.empty(1, dtype=rec_dtype)[0],
'bytearray': bytearray(3),
}
def setup():
"""
Precompile jitted functions. This will register many specializations
to choose from.
"""
from numba import jit
global binary, binary_pyobj, unary_default
@jit(nopython=True)
def binary(x, y):
pass
@jit(forceobj=True)
def binary_pyobj(x, y):
pass
@jit(nopython=True)
def unary_default(x=None):
pass
for tp in samples.values():
binary(tp, tp)
binary_pyobj(object(), object())
unary_default()
class NoPythonDispatch:
"""
Time dispatching to a jitted function's specializations based on argument
types.
This stresses two things:
- the typing of arguments (from argument value to typecode)
- the selection of the best specialization amongst all the known ones
"""
# We repeat 1000 times so as to make the overhead of benchmark launching
# negligible.
@classmethod
def generate_benchmarks(cls, names):
for name in names:
def timefunc(self, arg=samples[name]):
func = binary
for i in range(1000):
func(arg, arg)
timefunc.__name__ = "time_dispatch_" + name
setattr(cls, timefunc.__name__, timefunc)
def time_dispatch_defaults(self):
unary_default()
NoPythonDispatch.generate_benchmarks(samples.keys())
class PyObjectDispatch:
def time_dispatch_pyobject(self):
x = object()
for i in range(1000):
binary_pyobj(x, x)
You can’t perform that action at this time.