6
def compare_times(setup, expr):
7
print "Expression:", expr
9
exec setup in namespace
11
numpy_timer = timeit.Timer(expr, setup)
12
numpy_time = numpy_timer.timeit(number=iterations)
13
print 'numpy:', numpy_time / iterations
16
weave_timer = timeit.Timer('blitz("result=%s")' % expr, setup)
17
weave_time = weave_timer.timeit(number=iterations)
18
print "Weave:", weave_time/iterations
20
print "Speed-up of weave over numpy:", numpy_time/weave_time
22
print "Skipping weave timing"
24
numexpr_timer = timeit.Timer('evaluate("%s", optimization="aggressive")' % expr, setup)
25
numexpr_time = numexpr_timer.timeit(number=iterations)
26
print "numexpr:", numexpr_time/iterations
28
print "Speed-up of numexpr over numpy:", numpy_time/numexpr_time
29
return numpy_time/numexpr_time
32
from numpy import arange
33
try: from scipy.weave import blitz
35
from numexpr import evaluate
41
""" % ((array_size,)*5)
45
from numpy import arange
46
try: from scipy.weave import blitz
48
from numexpr import evaluate
52
""" % ((array_size,)*3)
57
from numpy import arange, sin, cos, sinh
58
try: from scipy.weave import blitz
60
from numexpr import evaluate
64
""" % ((array_size,)*3)
65
expr3 = '2*a + (cos(3)+5)*sinh(cos(b))'
69
from numpy import arange, sin, cos, sinh, arctan2
70
try: from scipy.weave import blitz
72
from numexpr import evaluate
76
""" % ((array_size,)*3)
77
expr4 = '2*a + arctan2(a, b)'
81
from numpy import arange, sin, cos, sinh, arctan2, sqrt, where
82
try: from scipy.weave import blitz
84
from numexpr import evaluate
85
a = arange(2*%f, dtype=float)[::2]
86
b = arange(%f, dtype=float)
87
result = arange(%f, dtype=float)
88
""" % ((array_size,)*3)
89
expr5 = 'where(0.1*a > arctan2(a, b), 2*a, arctan2(a,b))'
91
expr6 = 'where(a, 2, b)'
93
expr7 = 'where(a-10, a, 2)'
95
expr8 = 'where(a%2, b+5, 2)'
97
expr9 = 'where(a%2, 2, b+5)'
99
expr10 = 'a**2 + (b+1)**-2.5'
103
expr12 = 'sqrt(a**2 + b**2)'
105
def compare(check_only=False):
107
total += compare_times(setup1, expr1)
109
total += compare_times(setup2, expr2)
111
total += compare_times(setup3, expr3)
113
total += compare_times(setup4, expr4)
115
total += compare_times(setup5, expr6)
117
total += compare_times(setup5, expr7)
119
total += compare_times(setup5, expr8)
121
total += compare_times(setup5, expr9)
123
total += compare_times(setup5, expr10)
125
total += compare_times(setup5, expr11)
127
total += compare_times(setup5, expr12)
129
print "Average =", total / 11.0
132
if __name__ == '__main__':
135
averages.append(compare())
136
print "Averages:", ', '.join("%.2f" % x for x in averages)