From 180707958c1cf3d6ce418ec62299e0ba18c9a9b3 Mon Sep 17 00:00:00 2001 From: Dimitri Vorona Date: Mon, 3 Dec 2018 12:08:54 +0100 Subject: [PATCH 1/2] Add jitclass method benchmark --- benchmarks/bench_jitclass.py | 85 ++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 benchmarks/bench_jitclass.py diff --git a/benchmarks/bench_jitclass.py b/benchmarks/bench_jitclass.py new file mode 100644 index 0000000..b01f696 --- /dev/null +++ b/benchmarks/bench_jitclass.py @@ -0,0 +1,85 @@ +""" +Benchmarks for jitclass method dispatching and call overhead +""" + +import numpy as numpy + + +def setup(): + from numba import njit, jitclass, uint32 + + @jitclass([('val', uint32)]) + class Box: + def __init__(self, val): + self.val = val + + def inc(self): + self.val += 1 + + @property + def value(self): + return self.val + + @value.setter + def value(self, new_value): + self.val = new_value + + @njit + def method_call(N): + b = Box(0) + for i in range(N): + b.inc() + return b + + @njit + def property_access(N): + acc = 0 + b = Box(10) + for i in range(N): + acc += b.value + + return acc + + @njit + def property_setting(N): + b = Box(0) + for i in range(N): + b.value = i + + return b + + @njit + def constructor_call(N): + l = [] + for i in range(N): + l.append(Box(i)) + + return l + + globals().update(locals()) + + +class JitClassDispatch: + + N = 10 ** 8 + funcs = [ + 'constructor_call', + 'method_call', + 'property_access', + 'property_setting', + ] + + def setup(self): + for fn in self.funcs: + globals()[fn](1) + + @classmethod + def generate_benchmarks(cls): + for fn in cls.funcs: + fname = "time_{}".format(fn) + def f(self): + globals()[fn](self.N) + f.__name__ = fname + setattr(cls, fname, f) + +JitClassDispatch.generate_benchmarks() \ No newline at end of file From 5272d25068e9c0d2cf11df1e476b76e15e2f9886 Mon Sep 17 00:00:00 2001 From: Siu Kwan Lam Date: Mon, 7 Jan 2019 13:10:34 -0600 Subject: [PATCH 2/2] Add new line at the end --- benchmarks/bench_jitclass.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/benchmarks/bench_jitclass.py b/benchmarks/bench_jitclass.py index b01f696..84fd16b 100644 --- a/benchmarks/bench_jitclass.py +++ b/benchmarks/bench_jitclass.py @@ -82,4 +82,4 @@ def f(self): f.__name__ = fname setattr(cls, fname, f) -JitClassDispatch.generate_benchmarks() \ No newline at end of file +JitClassDispatch.generate_benchmarks()