summaryrefslogtreecommitdiff
path: root/benchmarks
diff options
context:
space:
mode:
authorStefan van der Walt <stefan@sun.ac.za>2007-02-17 18:36:16 +0000
committerStefan van der Walt <stefan@sun.ac.za>2007-02-17 18:36:16 +0000
commit5cb3d759d601455e7e94b59d1baaf167782f3544 (patch)
treefe5b4e0e8939d69fd9fbed8e6c91fc003d94102e /benchmarks
parent70c899f5940d2d2f99acbbc9906064e2028e25f9 (diff)
downloadpython-numpy-5cb3d759d601455e7e94b59d1baaf167782f3544.tar.gz
python-numpy-5cb3d759d601455e7e94b59d1baaf167782f3544.tar.bz2
python-numpy-5cb3d759d601455e7e94b59d1baaf167782f3544.zip
Refactor benchmarks.
Diffstat (limited to 'benchmarks')
-rw-r--r--benchmarks/benchmark.py42
-rw-r--r--benchmarks/casting.py24
-rw-r--r--benchmarks/creating.py21
-rw-r--r--benchmarks/sorting.py44
4 files changed, 90 insertions, 41 deletions
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()