def test_add(self): """ This seems to be about the amount of work to balance out the overhead by the overloaded one """ cr = compiler.compile_isolated(add, [types.int32]) cfunc = cr.entry_point disp = registry.CPUOverloaded(add) disp.add_overload(cr) x = 321 def python(): add(x) def pycfunc(): cfunc(x) def overloaded(): disp(x) print(add) print(utils.benchmark(python, maxsec=.5)) print(utils.benchmark(pycfunc, maxsec=.5)) print(utils.benchmark(overloaded, maxsec=.5))
def test_use_car_move(self): tyctx = typing.Context() tyctx.insert_class(Car, self.carattrs) cgctx = CPUContext(tyctx) cgctx.insert_class(Car, self.carattrs) car_object = types.Object(Car) argtys = (car_object, types.int32) flags = compiler.Flags() cr = compiler.compile_extra(tyctx, cgctx, use_car_move, args=argtys, return_type=None, flags=flags, locals={}) func = cr.entry_point if cr.typing_error: raise cr.typing_error car1 = Car(value=123) car2 = Car(value=123) self.assertEqual(use_car_move(car1, 321), func(car2, 321)) def bm_python(): use_car_move(car1, 321) def bm_numba(): func(car2, 321) python = utils.benchmark(bm_python, maxsec=.1) numba = utils.benchmark(bm_numba, maxsec=.1) print(python) print(numba)
def test_array_overhead(self): """ The time to set two array element seems to be more expensive than the overhead of the overloaded call. """ cr = compiler.compile_isolated(array_overhead, [types.int32[::1]]) cfunc = cr.entry_point disp = registry.CPUOverloaded(array_overhead) disp.add_overload(cr) self.assertEqual(cr.signature.args[0].layout, 'C') x = numpy.zeros(shape=2, dtype='int32') def python(): array_overhead(x) def pycfunc(): cfunc(x) def overloaded(): disp(x) print(array_overhead) print(utils.benchmark(python, maxsec=.5)) print(utils.benchmark(pycfunc, maxsec=.5)) print(utils.benchmark(overloaded, maxsec=.5))
def test_array_overhead(self): """ The time to set two array element seems to be more expensive than the overhead of the overloaded call. """ cr = compiler.compile_isolated(array_overhead, [types.int32[::1]]) cfunc = cr.entry_point disp = registry.CPUDispatcher(array_overhead) disp.add_overload(cr) self.assertEqual(cr.signature.args[0].layout, 'C') x = np.zeros(shape=2, dtype='int32') def python(): array_overhead(x) def pycfunc(): cfunc(x) def overloaded(): disp(x) print(array_overhead) print(utils.benchmark(python, maxsec=.5)) print(utils.benchmark(pycfunc, maxsec=.5)) print(utils.benchmark(overloaded, maxsec=.5))
def test_add(self): """ This seems to be about the amount of work to balance out the overhead by the overloaded one """ cr = compiler.compile_isolated(add, [types.int32]) cfunc = cr.entry_point disp = registry.CPUDispatcher(add) disp.add_overload(cr) x = 321 def python(): add(x) def pycfunc(): cfunc(x) def overloaded(): disp(x) print(add) print(utils.benchmark(python, maxsec=.5)) print(utils.benchmark(pycfunc, maxsec=.5)) print(utils.benchmark(overloaded, maxsec=.5))
def run(mod): name = mod.__name__[len(BENCHMARK_PREFIX):] print('running', name, end=' ...\n') bmr = benchmark(mod.python_main) python_best = bmr.best print('\tpython', python_best, 'seconds') bmr = benchmark(mod.numba_main) numba_best = bmr.best print('\tnumba', numba_best, 'seconds') print('\tspeedup', python_best / numba_best) return name, numba_best / python_best
def run(mod): name = mod.__name__[len(BENCHMARK_PREFIX) :] print("running", name, end=" ...\n") bmr = benchmark(mod.python_main) python_best = bmr.best print("\tpython", python_best, "seconds") bmr = benchmark(mod.numba_main) numba_best = bmr.best print("\tnumba", numba_best, "seconds") print("\tspeedup", python_best / numba_best) return name, numba_best / python_best
def test_loop_nest(self): """ Test bug that decref the iterator early. If the bug occurs, a segfault should occur """ pyfunc = loop_nest_3 cres = compile_isolated(pyfunc, (), flags=forceobj) cfunc = cres.entry_point self.assertEqual(pyfunc(5, 5), cfunc(5, 5)) def bm_pyfunc(): pyfunc(5, 5) def bm_cfunc(): cfunc(5, 5) print(utils.benchmark(bm_pyfunc)) print(utils.benchmark(bm_cfunc))
def test_sum1d_pyobj(self): pyfunc = usecases.sum1d cr = compile_isolated(pyfunc, (types.int32, types.int32), flags=force_pyobj_flags) cfunc = cr.entry_point ss = -1, 0, 1, 100, 200 es = -1, 0, 1, 100, 200 for args in itertools.product(ss, es): self.assertEqual(pyfunc(*args), cfunc(*args), args) args = 0, 500 def bm_python(): pyfunc(*args) def bm_numba(): cfunc(*args) print(utils.benchmark(bm_python, maxsec=.1)) print(utils.benchmark(bm_numba, maxsec=.1))
def test_binary_ufunc_performance(self): pyfunc = _make_binary_ufunc_usecase('add') arraytype = types.Array(types.float32, 1, 'C') cr = compile_isolated(pyfunc, (arraytype, arraytype, arraytype)) cfunc = cr.entry_point nelem = 5000 x_operand = np.arange(nelem, dtype=np.float32) y_operand = np.arange(nelem, dtype=np.float32) control = np.empty_like(x_operand) result = np.empty_like(x_operand) def bm_python(): pyfunc(x_operand, y_operand, control) def bm_numba(): cfunc(x_operand, y_operand, result) print(utils.benchmark(bm_python, maxsec=.1)) print(utils.benchmark(bm_numba, maxsec=.1)) assert np.allclose(control, result)
def test_overhead(self): """ This will show higher overhead due to unboxing in the native version. """ cr = compiler.compile_isolated(overhead, [types.int32]) cfunc = cr.entry_point disp = registry.CPUDispatcher(overhead) disp.add_overload(cr) x = 321 def python(): overhead(x) def pycfunc(): cfunc(x) def overloaded(): disp(x) print(overhead) print(utils.benchmark(python, maxsec=.5)) print(utils.benchmark(pycfunc, maxsec=.5)) print(utils.benchmark(overloaded, maxsec=.5))
def benchmark_refct_speed(): def pyfunc(x, y, t): """Swap array x and y for t number of times """ for i in range(t): x, y = y, x return x, y cfunc = nrtjit(pyfunc) x = np.random.random(100) y = np.random.random(100) t = 10000 def bench_pyfunc(): pyfunc(x, y, t) def bench_cfunc(): cfunc(x, y, t) python_time = utils.benchmark(bench_pyfunc) numba_time = utils.benchmark(bench_cfunc) print(python_time) print(numba_time)
def test_overhead(self): """ This will show higher overhead due to unboxing in the native version. """ cr = compiler.compile_isolated(overhead, [types.int32]) cfunc = cr.entry_point disp = registry.CPUOverloaded(overhead) disp.add_overload(cr) x = 321 def python(): overhead(x) def pycfunc(): cfunc(x) def overloaded(): disp(x) print(overhead) print(utils.benchmark(python, maxsec=.5)) print(utils.benchmark(pycfunc, maxsec=.5)) print(utils.benchmark(overloaded, maxsec=.5))
tol = 1.0e-6 error = 1.0 for j in range(n): A[j, 0] = 1.0 Anew[j, 0] = 1.0 it = 0 while error > tol and it < iter_max: error = fn(A, Anew) # swap A and Anew tmp = A A = Anew Anew = tmp it += 1 def python_main(): run(jocabi_relax_core) def numba_main(): run(numba_jocabi_relax_core) if __name__ == '__main__': print(benchmark(python_main)) print(benchmark(numba_main))
count += 2 return count @jit("intp()", nopython=True) def euler(): triangle = 1 index = 1 while factorCount(triangle) < 1001: index += 1 triangle += index return triangle answer = 842161320 def numba_main(): result = euler() assert result == answer def python_main(): result = py_euler() assert result == answer if __name__ == '__main__': print(benchmark(python_main)) print(benchmark(numba_main))