1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
###################################################################
# Numexpr - Fast numerical array expression evaluator for NumPy.
#
# License: MIT
# Author: See AUTHORS.txt
#
# See LICENSE.txt and LICENSES/*.txt for details about copyright and
# rights to use.
####################################################################
# Script to check that multidimensional arrays are speed-up properly too
# Based on a script provided by Andrew Collette.
from __future__ import print_function
import time
import numpy as np
import numexpr as nx
test_shapes = [
(100*100*100),
(100*100,100),
(100,100,100),
]
test_dtype = 'f4'
nruns = 10 # Ensemble for timing
def chunkify(chunksize):
""" Very stupid "chunk vectorizer" which keeps memory use down.
This version requires all inputs to have the same number of elements,
although it shouldn't be that hard to implement simple broadcasting.
"""
def chunkifier(func):
def wrap(*args):
assert len(args) > 0
assert all(len(a.flat) == len(args[0].flat) for a in args)
nelements = len(args[0].flat)
nchunks, remain = divmod(nelements, chunksize)
out = np.ndarray(args[0].shape)
for start in range(0, nelements, chunksize):
#print(start)
stop = start+chunksize
if start+chunksize > nelements:
stop = nelements-start
iargs = tuple(a.flat[start:stop] for a in args)
out.flat[start:stop] = func(*iargs)
return out
return wrap
return chunkifier
test_func_str = "63 + (a*b) + (c**2) + b"
def test_func(a, b, c):
return 63 + (a*b) + (c**2) + b
test_func_chunked = chunkify(100*100)(test_func)
for test_shape in test_shapes:
test_size = np.product(test_shape)
# The actual data we'll use
a = np.arange(test_size, dtype=test_dtype).reshape(test_shape)
b = np.arange(test_size, dtype=test_dtype).reshape(test_shape)
c = np.arange(test_size, dtype=test_dtype).reshape(test_shape)
start1 = time.time()
for idx in range(nruns):
result1 = test_func(a, b, c)
stop1 = time.time()
start2 = time.time()
for idx in range(nruns):
result2 = nx.evaluate(test_func_str)
stop2 = time.time()
start3 = time.time()
for idx in range(nruns):
result3 = test_func_chunked(a, b, c)
stop3 = time.time()
print("%s %s (average of %s runs)" % (test_shape, test_dtype, nruns))
print("Simple: ", (stop1-start1)/nruns)
print("Numexpr: ", (stop2-start2)/nruns)
print("Chunked: ", (stop3-start3)/nruns)
|