From 5cb3d759d601455e7e94b59d1baaf167782f3544 Mon Sep 17 00:00:00 2001 From: Stefan van der Walt Date: Sat, 17 Feb 2007 18:36:16 +0000 Subject: Refactor benchmarks. --- benchmarks/benchmark.py | 42 ++++++++++++++++++++++++++++++++++++++++++ benchmarks/casting.py | 24 ++++++++++++++++-------- benchmarks/creating.py | 21 +++++++++++++-------- benchmarks/sorting.py | 44 +++++++++++++++++++------------------------- 4 files changed, 90 insertions(+), 41 deletions(-) create mode 100644 benchmarks/benchmark.py (limited to 'benchmarks') diff --git a/benchmarks/benchmark.py b/benchmarks/benchmark.py new file mode 100644 index 000000000..59e464686 --- /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 N; ' % (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 index cb498db10..5624fddfa 100644 --- a/benchmarks/casting.py +++ b/benchmarks/casting.py @@ -1,9 +1,17 @@ -import timeit +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] -t1 = timeit.Timer('b = a.astype(int)','import numpy;a=numpy.zeros(shape=%s,dtype=float)'%N) -t2 = timeit.Timer('b = a.astype("l")','import Numeric;a=Numeric.zeros(shape=%s,typecode="d")'%N) -t3 = timeit.Timer("b = a.astype('l')","import numarray; a=numarray.zeros(shape=%s,typecode='d')"%N) -print "1-D length = ", N -print "NumPy: ", t1.repeat(3,1000) -print "Numeric: ", t2.repeat(3,1000) -print "Numarray: ", t3.repeat(3,1000) +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 index 462d2cd19..981c7fe47 100644 --- a/benchmarks/creating.py +++ b/benchmarks/creating.py @@ -1,9 +1,14 @@ -import timeit +from benchmark import Benchmark + +modules = ['numpy','Numeric','numarray'] + N = [10,10] -t1 = timeit.Timer('a=N.zeros(shape,type)','import numpy as N; shape=%s;type=float'%N) -t2 = timeit.Timer('a=N.zeros(shape,type)','import Numeric as N; shape=%s;type=N.Float'%N) -t3 = timeit.Timer('a=N.zeros(shape,type)',"import numarray as N; shape=%s;type=N.Float"%N) -print "shape = ", N -print "NumPy: ", t1.repeat(3,10000) -print "Numeric: ", t2.repeat(3,10000) -print "Numarray: ", t3.repeat(3,10000) +b = Benchmark(modules, + title='Creating %s zeros.' % N, + runs=3,reps=10000) + +b['numpy'] = ('a=N.zeros(shape,type)', 'shape=%s;type=float' % N) +b['Numeric'] = ('a=N.zeros(shape,type)', 'shape=%s;type=N.Float' % N) +b['numarray'] = ('a=N.zeros(shape,type)', "shape=%s;type=N.Float" % N) + +b.run() diff --git a/benchmarks/sorting.py b/benchmarks/sorting.py index 23d58983a..8f88896e8 100644 --- a/benchmarks/sorting.py +++ b/benchmarks/sorting.py @@ -1,31 +1,25 @@ -import timeit +from benchmark import Benchmark -N = 10000 -t1 = timeit.Timer('a=array(None,shape=%d);a.sort()'%N,'from numarray import array') -t2 = timeit.Timer('a=empty(shape=%d);a.sort()'%N,'from numpy import empty') -t3 = timeit.Timer('a=empty(shape=%d);sort(a)'%N,'from Numeric import empty,sort') +modules = ['numpy','Numeric','numarray'] +b = Benchmark(modules,runs=3,reps=100) -print "1-D length = ", N -print "Numarray: ", t1.repeat(3,100) -print "NumPy: ", t2.repeat(3,100) -print "Numeric: ", t3.repeat(3,100) +N = 10000 +b.title = 'Sorting %d elements' % N +b['numarray'] = ('a=N.array(None,shape=%d);a.sort()'%N,'') +b['numpy'] = ('a=N.empty(shape=%d);a.sort()'%N,'') +b['Numeric'] = ('a=N.empty(shape=%d);N.sort(a)'%N,'') +b.run() N1,N2 = 100,100 -t1 = timeit.Timer('a=array(None,shape=(%d,%d));a.sort()'%(N1,N2),'from numarray import array') -t2 = timeit.Timer('a=empty(shape=(%d,%d));a.sort()'%(N1,N2),'from numpy import empty') -t3 = timeit.Timer('a=empty(shape=(%d,%d));sort(a)'%(N1,N2),'from Numeric import empty,sort') - -print "2-D shape = (%d,%d), last-axis" % (N1,N2) -print "Numarray: ", t1.repeat(3,100) -print "NumPy: ", t2.repeat(3,100) -print "Numeric: ", t3.repeat(3,100) +b.title = 'Sorting (%d,%d) elements, last axis' % (N1,N2) +b['numarray'] = ('a=N.array(None,shape=(%d,%d));a.sort()'%(N1,N2),'') +b['numpy'] = ('a=N.empty(shape=(%d,%d));a.sort()'%(N1,N2),'') +b['Numeric'] = ('a=N.empty(shape=(%d,%d));N.sort(a)'%(N1,N2),'') +b.run() N1,N2 = 100,100 -t1 = timeit.Timer('a=array(None,shape=(%d,%d));a.sort(0)'%(N1,N2),'from numarray import array') -t2 = timeit.Timer('a=empty(shape=(%d,%d));a.sort(0)'%(N1,N2),'from numpy import empty') -t3 = timeit.Timer('a=empty(shape=(%d,%d));sort(a,0)'%(N1,N2),'from Numeric import empty,sort') - -print "2-D shape = (%d,%d), first-axis" % (N1,N2) -print "Numarray: ", t1.repeat(3,100) -print "NumPy: ", t2.repeat(3,100) -print "Numeric: ", t3.repeat(3,100) +b.title = 'Sorting (%d,%d) elements, first axis' % (N1,N2) +b['numarray'] = ('a=N.array(None,shape=(%d,%d));a.sort(0)'%(N1,N2),'') +b['Numeric'] = ('a=N.empty(shape=(%d,%d));N.sort(a,0)'%(N1,N2),'') +b['numpy'] = ('a=N.empty(shape=(%d,%d));N.sort(a,0)'%(N1,N2),'') +b.run() -- cgit v1.2.3