summaryrefslogtreecommitdiff
path: root/benchmarks/simpleindex.py
blob: 07c57f5068731f74549ebaab8196e5806b0b4520 (plain)
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
import timeit
# This is to show that NumPy is a poorer choice than nested Python lists
#   if you are writing nested for loops.
# This is slower than Numeric was but Numeric was slower than Python lists were
#   in the first place. 

N = 30
code2 = r"""
for k in xrange(%d):
    for l in xrange(%d):
        res = a[k,l].item() + a[l,k].item()
""" % (N,N)
code3 = r"""
for k in xrange(%d):
    for l in xrange(%d):
        res = a[k][l] + a[l][k]
""" % (N,N)
code = r"""
for k in xrange(%d):
    for l in xrange(%d):
        res = a[k,l] + a[l,k]
""" % (N,N)
setup3 = r"""
import random
a = [[None for k in xrange(%d)] for l in xrange(%d)]
for k in xrange(%d):
    for l in xrange(%d):
        a[k][l] = random.random()
""" % (N,N,N,N)
t1 = timeit.Timer(code, 'import numpy as N; a = N.rand(%d,%d)' % (N,N))
t2 = timeit.Timer(code, 'import MLab as N; a=N.rand(%d,%d)' % (N,N))
t3 = timeit.Timer(code, 'import numarray.mlab as N; a=N.rand(%d,%d)' % (N,N))
t4 = timeit.Timer(code2, 'import numpy as N; a = N.rand(%d,%d)' % (N,N))
t5 = timeit.Timer(code3, setup3)
t6 = timeit.Timer("res = a + a.transpose()","import numpy as N; a=N.rand(%d,%d)" % (N,N))
print "shape = ", (N,N)
print "NumPy 1: ", t1.repeat(3,100)
print "NumPy 2: ", t4.repeat(3,100)
print "Numeric: ", t2.repeat(3,100)
print "Numarray: ", t3.repeat(3,100)
print "Python: ", t5.repeat(3,100)
print "Optimized: ", t6.repeat(3,100)