[Scipy-svn] r4516 - trunk/scipy/sandbox/mkufunc/examples

scipy-svn@scip... scipy-svn@scip...
Tue Jul 1 11:24:20 CDT 2008


Author: ilan
Date: 2008-07-01 11:24:20 -0500 (Tue, 01 Jul 2008)
New Revision: 4516

Modified:
   trunk/scipy/sandbox/mkufunc/examples/benchmark.py
Log:
Added hand written weave version (slightly faster)

Modified: trunk/scipy/sandbox/mkufunc/examples/benchmark.py
===================================================================
--- trunk/scipy/sandbox/mkufunc/examples/benchmark.py	2008-07-01 14:20:07 UTC (rev 4515)
+++ trunk/scipy/sandbox/mkufunc/examples/benchmark.py	2008-07-01 16:24:20 UTC (rev 4516)
@@ -14,11 +14,77 @@
 
 vfunc = vectorize(f)
 
-ufunc = mkufunc([(float, float)])(f)
+mfunc = mkufunc([(float, float)])(f)
 
+#####################################################################
+support_code = '''
+double foo(double x)
+{
+        return x;
+}
 
-x = arange(0, 1000, 0.001)    #print "x =", x, x.dtype
+typedef double Func_0(double a);
 
+static void
+PyUFunc_0(char **args, npy_intp *dimensions, npy_intp *steps, void *func)
+{
+        npy_intp i, n;
+        npy_intp is0 = steps[0];
+        npy_intp os = steps[1];
+        char *ip0 = args[0];
+        char *op = args[1];
+        Func_0 *f = (Func_0 *) func;
+        n = dimensions[0];
+        
+        for(i = 0; i < n; i++) {
+                double x = (double *)ip0;
+                double *out = (double *)op;
+
+                *out = 4.2 * x*x + 3.7 * x + 1.5;
+
+                ip0 += is0;
+                op += os;
+        }
+}
+
+static PyUFuncGenericFunction f_functions[] = {
+        PyUFunc_0,
+};
+
+static void *f_data[] = {
+        (void *) foo,
+};
+
+static char f_types[] = {
+        NPY_DOUBLE, NPY_DOUBLE,   /* 0 */
+};
+'''
+ufunc_info = weave.base_info.custom_info()
+ufunc_info.add_header('"numpy/ufuncobject.h"')
+
+ufunc = weave.inline('''
+import_ufunc();
+
+return_val = PyUFunc_FromFuncAndData(
+            f_functions,
+            f_data,
+            f_types,
+            1,             /* ntypes */
+            1,             /* nin */
+            1,             /* nout */
+            PyUFunc_None,  /* identity */
+            "f",           /* name */
+            "doc",         /* doc */
+            0);
+            ''',
+                     support_code=support_code,
+                     verbose=0,
+                     customize=ufunc_info)
+#############################################################
+
+
+x = arange(0, 1000, 0.0001)    #print "x =", x, x.dtype
+
 start_time = time.time()
 b_y = x.copy()
 weave.blitz("b_y[:] = 4.2 * x[:] * x[:] + 3.7 * x[:] + 1.5")
@@ -36,14 +102,21 @@
 print 'vectorize: %.6f sec' % v_time
 
 start_time = time.time()
+m_y = mfunc(x)
+m_time = time.time() - start_time
+print 'mkufunc: %.6f sec' % m_time
+
+start_time = time.time()
 u_y = ufunc(x)
 u_time = time.time() - start_time
-print 'mkufunc: %.6f sec' % u_time
+print 'ufunc: %.6f sec' % u_time
 
-print "speedup over blitz:",     b_time/u_time
-print "speedup over numpy:",     n_time/u_time
-print "speedup over vectorize:", v_time/u_time
+print "speedup over blitz:",     b_time/m_time
+print "speedup over numpy:",     n_time/m_time
+print "speedup over vectorize:", v_time/m_time
+print "speedup over ufunc:", u_time/m_time
 
 assert allclose(b_y, n_y)
 assert allclose(v_y, n_y)
+assert allclose(m_y, n_y)
 assert allclose(u_y, n_y)



More information about the Scipy-svn mailing list