Skip to content

Commit

Permalink
Merge pull request #8 from alendit/jitclass_benchmark
Browse files Browse the repository at this point in the history
Add jitclass method benchmark
  • Loading branch information
sklam committed Jan 7, 2019
2 parents 5e7966c + 5272d25 commit 065ebb1
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions 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()

0 comments on commit 065ebb1

Please sign in to comment.