[Numpy-svn] r3547 - trunk/benchmarks

numpy-svn@scip... numpy-svn@scip...
Sat Feb 17 12:36:29 CST 2007


Author: stefan
Date: 2007-02-17 12:36:16 -0600 (Sat, 17 Feb 2007)
New Revision: 3547

Added:
   trunk/benchmarks/benchmark.py
Modified:
   trunk/benchmarks/casting.py
   trunk/benchmarks/creating.py
   trunk/benchmarks/sorting.py
Log:
Refactor benchmarks.


Added: trunk/benchmarks/benchmark.py
===================================================================
--- trunk/benchmarks/benchmark.py	2007-02-10 00:43:37 UTC (rev 3546)
+++ trunk/benchmarks/benchmark.py	2007-02-17 18:36:16 UTC (rev 3547)
@@ -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

Modified: trunk/benchmarks/casting.py
===================================================================
--- trunk/benchmarks/casting.py	2007-02-10 00:43:37 UTC (rev 3546)
+++ trunk/benchmarks/casting.py	2007-02-17 18:36:16 UTC (rev 3547)
@@ -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()

Modified: trunk/benchmarks/creating.py
===================================================================
--- trunk/benchmarks/creating.py	2007-02-10 00:43:37 UTC (rev 3546)
+++ trunk/benchmarks/creating.py	2007-02-17 18:36:16 UTC (rev 3547)
@@ -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()

Modified: trunk/benchmarks/sorting.py
===================================================================
--- trunk/benchmarks/sorting.py	2007-02-10 00:43:37 UTC (rev 3546)
+++ trunk/benchmarks/sorting.py	2007-02-17 18:36:16 UTC (rev 3547)
@@ -1,31 +1,25 @@
-import timeit
+from benchmark import Benchmark
 
+modules = ['numpy','Numeric','numarray']
+b = Benchmark(modules,runs=3,reps=100)
+
 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')
+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()
 
-print "1-D length = ", N
-print "Numarray: ", t1.repeat(3,100)
-print "NumPy: ", t2.repeat(3,100)
-print "Numeric: ", t3.repeat(3,100)
-
 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')
+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()
 
-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)
-
 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()



More information about the Numpy-svn mailing list