diff options
Diffstat (limited to 'benchmarks')
-rw-r--r-- | benchmarks/benchmark.py | 42 | ||||
-rw-r--r-- | benchmarks/casting.py | 17 | ||||
-rw-r--r-- | benchmarks/creating.py | 14 | ||||
-rw-r--r-- | benchmarks/simpleindex.py | 48 | ||||
-rw-r--r-- | benchmarks/sorting.py | 25 |
5 files changed, 146 insertions, 0 deletions
diff --git a/benchmarks/benchmark.py b/benchmarks/benchmark.py new file mode 100644 index 000000000..526a69d58 --- /dev/null +++ b/benchmarks/benchmark.py @@ -0,0 +1,42 @@ +from timeit import Timer + +class Benchmark(dict): + """Benchmark a feature in different modules.""" + + def __init__(self,modules,title='',runs=3,reps=1000): + self.module_test = dict((m,'') for m in modules) + self.runs = runs + self.reps = reps + self.title = title + + def __setitem__(self,module,(test_str,setup_str)): + """Set the test code for modules.""" + if module == 'all': + modules = self.module_test.keys() + else: + modules = [module] + + for m in modules: + setup_str = 'import %s; import %s as np; ' % (m,m) \ + + setup_str + self.module_test[m] = Timer(test_str, setup_str) + + def run(self): + """Run the benchmark on the different modules.""" + module_column_len = max(len(mod) for mod in self.module_test) + + if self.title: + print self.title + print 'Doing %d runs, each with %d reps.' % (self.runs,self.reps) + print '-'*79 + + for mod in sorted(self.module_test): + modname = mod.ljust(module_column_len) + try: + print "%s: %s" % (modname, \ + self.module_test[mod].repeat(self.runs,self.reps)) + except Exception, e: + print "%s: Failed to benchmark (%s)." % (modname,e) + + print '-'*79 + print diff --git a/benchmarks/casting.py b/benchmarks/casting.py new file mode 100644 index 000000000..5624fddfa --- /dev/null +++ b/benchmarks/casting.py @@ -0,0 +1,17 @@ +from benchmark import Benchmark + +modules = ['numpy','Numeric','numarray'] + +b = Benchmark(modules, + title='Casting a (10,10) integer array to float.', + runs=3,reps=10000) + +N = [10,10] +b['numpy'] = ('b = a.astype(int)', + 'a=numpy.zeros(shape=%s,dtype=float)' % N) +b['Numeric'] = ('b = a.astype("l")', + 'a=Numeric.zeros(shape=%s,typecode="d")' % N) +b['numarray'] = ("b = a.astype('l')", + "a=numarray.zeros(shape=%s,typecode='d')" % N) + +b.run() diff --git a/benchmarks/creating.py b/benchmarks/creating.py new file mode 100644 index 000000000..6f8dc0217 --- /dev/null +++ b/benchmarks/creating.py @@ -0,0 +1,14 @@ +from benchmark import Benchmark + +modules = ['numpy','Numeric','numarray'] + +N = [10,10] +b = Benchmark(modules, + title='Creating %s zeros.' % N, + runs=3,reps=10000) + +b['numpy'] = ('a=np.zeros(shape,type)', 'shape=%s;type=float' % N) +b['Numeric'] = ('a=np.zeros(shape,type)', 'shape=%s;type=np.Float' % N) +b['numarray'] = ('a=np.zeros(shape,type)', "shape=%s;type=np.Float" % N) + +b.run() diff --git a/benchmarks/simpleindex.py b/benchmarks/simpleindex.py new file mode 100644 index 000000000..e4e541d96 --- /dev/null +++ b/benchmarks/simpleindex.py @@ -0,0 +1,48 @@ +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) + +numpy_timer1 = timeit.Timer(code, 'import numpy as np; a = np.random.rand(%d,%d)' % (N,N)) +numeric_timer = timeit.Timer(code, 'import MLab as np; a=np.rand(%d,%d)' % (N,N)) +numarray_timer = timeit.Timer(code, 'import numarray.mlab as np; a=np.rand(%d,%d)' % (N,N)) +numpy_timer2 = timeit.Timer(code2, 'import numpy as np; a = np.random.rand(%d,%d)' % (N,N)) +python_timer = timeit.Timer(code3, setup3) +numpy_timer3 = timeit.Timer("res = a + a.transpose()","import numpy as np; a=np.random.rand(%d,%d)" % (N,N)) + +print "shape = ", (N,N) +print "NumPy 1: ", numpy_timer1.repeat(3,100) +print "NumPy 2: ", numpy_timer2.repeat(3,100) +print "Numeric: ", numeric_timer.repeat(3,100) +print "Numarray: ", numarray_timer.repeat(3,100) +print "Python: ", python_timer.repeat(3,100) +print "Optimized: ", numpy_timer3.repeat(3,100) diff --git a/benchmarks/sorting.py b/benchmarks/sorting.py new file mode 100644 index 000000000..5a23506b5 --- /dev/null +++ b/benchmarks/sorting.py @@ -0,0 +1,25 @@ +from benchmark import Benchmark + +modules = ['numpy','Numeric','numarray'] +b = Benchmark(modules,runs=3,reps=100) + +N = 10000 +b.title = 'Sorting %d elements' % N +b['numarray'] = ('a=np.array(None,shape=%d,typecode="i");a.sort()'%N,'') +b['numpy'] = ('a=np.empty(shape=%d, dtype="i");a.sort()'%N,'') +b['Numeric'] = ('a=np.empty(shape=%d, typecode="i");np.sort(a)'%N,'') +b.run() + +N1,N2 = 100,100 +b.title = 'Sorting (%d,%d) elements, last axis' % (N1,N2) +b['numarray'] = ('a=np.array(None,shape=(%d,%d),typecode="i");a.sort()'%(N1,N2),'') +b['numpy'] = ('a=np.empty(shape=(%d,%d), dtype="i");a.sort()'%(N1,N2),'') +b['Numeric'] = ('a=np.empty(shape=(%d,%d),typecode="i");np.sort(a)'%(N1,N2),'') +b.run() + +N1,N2 = 100,100 +b.title = 'Sorting (%d,%d) elements, first axis' % (N1,N2) +b['numarray'] = ('a=np.array(None,shape=(%d,%d), typecode="i");a.sort(0)'%(N1,N2),'') +b['numpy'] = ('a=np.empty(shape=(%d,%d),dtype="i");np.sort(a,0)'%(N1,N2),'') +b['Numeric'] = ('a=np.empty(shape=(%d,%d),typecode="i");np.sort(a,0)'%(N1,N2),'') +b.run() |