~ubuntu-branches/ubuntu/lucid/python-scipy/lucid

« back to all changes in this revision

Viewing changes to Lib/sandbox/numexpr/timing.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-01-07 14:12:12 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20070107141212-mm0ebkh5b37hcpzn
* Remove build dependency on python-numpy-dev.
* python-scipy: Depend on python-numpy instead of python-numpy-dev.
* Package builds on other archs than i386. Closes: #402783.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import timeit, numpy
 
2
 
 
3
array_size = 1e6
 
4
iterations = 10
 
5
 
 
6
def compare_times(setup, expr):
 
7
    print "Expression:", expr
 
8
    namespace = {}
 
9
    exec setup in namespace
 
10
 
 
11
    numpy_timer = timeit.Timer(expr, setup)
 
12
    numpy_time = numpy_timer.timeit(number=iterations)
 
13
    print 'numpy:', numpy_time / iterations
 
14
 
 
15
    try:
 
16
        weave_timer = timeit.Timer('blitz("result=%s")' % expr, setup)
 
17
        weave_time = weave_timer.timeit(number=iterations)
 
18
        print "Weave:", weave_time/iterations
 
19
 
 
20
        print "Speed-up of weave over numpy:", numpy_time/weave_time
 
21
    except:
 
22
        print "Skipping weave timing"
 
23
 
 
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
 
27
 
 
28
    print "Speed-up of numexpr over numpy:", numpy_time/numexpr_time
 
29
    return numpy_time/numexpr_time
 
30
 
 
31
setup1 = """\
 
32
from numpy import arange
 
33
try: from scipy.weave import blitz
 
34
except: pass
 
35
from numexpr import evaluate
 
36
result = arange(%f)
 
37
b = arange(%f)
 
38
c = arange(%f)
 
39
d = arange(%f)
 
40
e = arange(%f)
 
41
""" % ((array_size,)*5)
 
42
expr1 = 'b*c+d*e'
 
43
 
 
44
setup2 = """\
 
45
from numpy import arange
 
46
try: from scipy.weave import blitz
 
47
except: pass
 
48
from numexpr import evaluate
 
49
a = arange(%f)
 
50
b = arange(%f)
 
51
result = arange(%f)
 
52
""" % ((array_size,)*3)
 
53
expr2 = '2*a+3*b'
 
54
 
 
55
 
 
56
setup3 = """\
 
57
from numpy import arange, sin, cos, sinh
 
58
try: from scipy.weave import blitz
 
59
except: pass
 
60
from numexpr import evaluate
 
61
a = arange(2*%f)[::2]
 
62
b = arange(%f)
 
63
result = arange(%f)
 
64
""" % ((array_size,)*3)
 
65
expr3 = '2*a + (cos(3)+5)*sinh(cos(b))'
 
66
 
 
67
 
 
68
setup4 = """\
 
69
from numpy import arange, sin, cos, sinh, arctan2
 
70
try: from scipy.weave import blitz
 
71
except: pass
 
72
from numexpr import evaluate
 
73
a = arange(2*%f)[::2]
 
74
b = arange(%f)
 
75
result = arange(%f)
 
76
""" % ((array_size,)*3)
 
77
expr4 = '2*a + arctan2(a, b)'
 
78
 
 
79
 
 
80
setup5 = """\
 
81
from numpy import arange, sin, cos, sinh, arctan2, sqrt, where
 
82
try: from scipy.weave import blitz
 
83
except: pass
 
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))'
 
90
 
 
91
expr6 = 'where(a, 2, b)'
 
92
 
 
93
expr7 = 'where(a-10, a, 2)'
 
94
 
 
95
expr8 = 'where(a%2, b+5, 2)'
 
96
 
 
97
expr9 = 'where(a%2, 2, b+5)'
 
98
 
 
99
expr10 = 'a**2 + (b+1)**-2.5'
 
100
 
 
101
expr11 = '(a+1)**50'
 
102
 
 
103
expr12 = 'sqrt(a**2 + b**2)'
 
104
 
 
105
def compare(check_only=False):
 
106
    total = 0
 
107
    total += compare_times(setup1, expr1)
 
108
    print
 
109
    total += compare_times(setup2, expr2)
 
110
    print
 
111
    total += compare_times(setup3, expr3)
 
112
    print
 
113
    total += compare_times(setup4, expr4)
 
114
    print
 
115
    total += compare_times(setup5, expr6)
 
116
    print
 
117
    total += compare_times(setup5, expr7)
 
118
    print
 
119
    total += compare_times(setup5, expr8)
 
120
    print
 
121
    total += compare_times(setup5, expr9)
 
122
    print
 
123
    total += compare_times(setup5, expr10)
 
124
    print
 
125
    total += compare_times(setup5, expr11)
 
126
    print
 
127
    total += compare_times(setup5, expr12)
 
128
    print
 
129
    print "Average =", total / 11.0
 
130
    return total
 
131
 
 
132
if __name__ == '__main__':
 
133
    averages = []
 
134
    for i in range(10):
 
135
        averages.append(compare())
 
136
    print "Averages:", ', '.join("%.2f" % x for x in averages)