-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
142 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,92 @@ | ||
|
||
import time, os | ||
from jitpy import setup, extra_source | ||
setup(os.environ['PYPY_HOME']) | ||
from jitpy.wrapper import jittify | ||
import numba, numpy | ||
|
||
ARRAY_SIZE = 10000 | ||
NUMBER = 1000 | ||
|
||
def f(a, s): | ||
for k in range(10): | ||
for i in xrange(a.shape[0]): | ||
s += a[i] | ||
return s | ||
|
||
def prepare_f(): | ||
return numpy.arange(ARRAY_SIZE) | ||
|
||
def f2(a, s): | ||
for k in range(10): | ||
for i in xrange(a.shape[0]): | ||
for j in xrange(a.shape[1]): | ||
s += a[i, j] | ||
return s | ||
|
||
def prepare_f2(): | ||
return numpy.arange(ARRAY_SIZE).reshape(100, 100) | ||
|
||
def f3(a, s): | ||
for k in range(10): | ||
for i in xrange(a.shape[0] - 1): | ||
for j in xrange(a.shape[1]): | ||
s += len((a[i, j], a[i + 1, j])) | ||
return s | ||
|
||
def prepare_f3(): | ||
return numpy.arange(ARRAY_SIZE).reshape(100, 100) | ||
|
||
def f4(a, s): | ||
for k in range(10): | ||
for i in xrange(a.shape[0] - 1): | ||
for j in xrange(a.shape[1]): | ||
s += X(a[i, j]).x | ||
return s | ||
|
||
def prepare_f4(): | ||
return numpy.arange(ARRAY_SIZE).reshape(100, 100) | ||
|
||
def check_py(f, prepare): | ||
a = prepare() | ||
for i in xrange(NUMBER / 10): | ||
f(a, 3.5) | ||
|
||
|
||
def check_jitpy(f, prepare): | ||
jit_f = jittify(['array', float], float)(f) | ||
a = prepare() | ||
for i in xrange(NUMBER): | ||
jit_f(a, 3.5) | ||
|
||
def check_numba(f, prepare): | ||
jit_f = numba.jit(f) | ||
a = prepare() | ||
for i in xrange(NUMBER): | ||
jit_f(a, 3.5) | ||
|
||
class X(object): | ||
def __init__(self, x): | ||
self.x = x | ||
|
||
extra_source(""" | ||
class X(object): | ||
def __init__(self, x): | ||
self.x = x | ||
""") | ||
|
||
ALL = [('PY', check_py), ('JITPY', check_jitpy), ('NUMBA', check_numba)] | ||
|
||
for bench_name, bench_func, prep_func in [ | ||
('array traversal', f, prepare_f), | ||
('2d array traversal', f2, prepare_f2), | ||
('tuple + array', f3, prepare_f3), | ||
('instance creation', f4, prepare_f4)]: | ||
print bench_name | ||
for name, func in ALL: | ||
t0 = time.time() | ||
func(bench_func, prep_func) | ||
t = time.time() - t0 | ||
if func is check_py: | ||
t *= 10 | ||
print name, t |
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,50 @@ | ||
|
||
import time, os | ||
try: | ||
from jitpy import setup | ||
import numba | ||
CPYTHON = True | ||
except: | ||
CPYTHON = False | ||
else: | ||
setup(os.environ['PYPY_HOME']) | ||
from jitpy.wrapper import jittify | ||
|
||
NUMBER = 1000000 | ||
|
||
def f(a, b, c): | ||
return 1 | ||
|
||
def f2(a, b, c): | ||
s = 0 | ||
for i in xrange(a): | ||
s += a | ||
s += b | ||
s += c | ||
return s | ||
|
||
def check_py(f): | ||
for i in xrange(NUMBER): | ||
f(10, i, i) | ||
|
||
def check_jitpy(f): | ||
jit_f = jittify([int, int, int], int)(f) | ||
for i in xrange(NUMBER): | ||
jit_f(10, i, i) | ||
|
||
def check_numba(f): | ||
jit_f = numba.jit(f) | ||
for i in xrange(NUMBER): | ||
jit_f(10, i, i) | ||
|
||
if CPYTHON: | ||
ALL = [('PY', check_py), ('JITPY', check_jitpy), ('NUMBA', check_numba)] | ||
else: | ||
ALL = [('PY', check_py)] | ||
|
||
for bench_name, bench_func in [('basic', f), ('basic_loop', f2)]: | ||
print bench_name | ||
for name, func in ALL: | ||
t0 = time.time() | ||
func(bench_func) | ||
print name, time.time() - t0 |