Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from alendit/jitclass_benchmark
Add jitclass method benchmark
- Loading branch information
Showing
1 changed file
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |