On Apr 19, 2008, at 5:42 PM, Gabriel Gellner wrote:
I was reading an old article by David Mertz on pyrex
(http://www.ibm.com/developerworks/linux/library/l-cppyrex.html)
were he finds that a program he wrote in python becomes slower
we naively fed into pyrex (no changes, though he was forced to change
list comprehensions, and inplace arithmetic).

These are available in Cython.

So I redid the test, and with no changes at all, the compiled code runs
around 15% faster (as opposed to 20$ slower).

Thought people might be interested . . . Made me happy.

Cool, thanks for posting. He is right (see attached) that the bottleneck is all the Python stuff that is done in the very center loop (string addition, calling sha, etc.) which could be made a little faster, but no matter what language this is written in actually computing sha should overwhelm loop overhead, and since most of the time is spent in an external library I'm not sure how good of an example this is.

- Robert



Generated by Cython 0.9.6.13 on Sat Apr 19 18:33:24 2008

Raw output: _Users_robert__sage_sage_notebook_worksheets_admin_74_code_sage12_spyx_0.c

 1: 
 2: include "interrupt.pxi"  # ctrl-c interrupt block support
 3: include "stdsage.pxi"  # ctrl-c interrupt block support
 4: 
 5: include "cdefs.pxi"
 6: cdef _mint(char *challenge, int bits):
static PyObject *__pyx_n_alphabet;
static PyObject *__pyx_n_ceil;
static PyObject *__pyx_n_sha;
static PyObject *__pyx_n_hexdigest;

static char __pyx_k_1[] = "******";

static  PyObject *__pyx_f_72_Users_robert__sage_sage_notebook_worksheets_admin_74_code_sage12_spyx_0__mint(char *__pyx_v_challenge, int __pyx_v_bits) {
  int __pyx_v_hex_digits;
  int __pyx_v_i0;
  int __pyx_v_i1;
  int __pyx_v_i2;
  int __pyx_v_i3;
  int __pyx_v_i4;
  int __pyx_v_i5;
  char *__pyx_v_ab;
  char *__pyx_v_digest;
  char *__pyx_v_suffix;
  PyObject *__pyx_v_hash;
  PyObject *__pyx_v_py_digest;
  PyObject *__pyx_v_i;
  PyObject *__pyx_r;
  PyObject *__pyx_1 = 0;
  char *__pyx_2;
  PyObject *__pyx_3 = 0;
  PyObject *__pyx_4 = 0;
  int __pyx_5;
  long __pyx_6;
  Py_ssize_t __pyx_7 = 0;
  int __pyx_8;
  __pyx_v_hash = Py_None; Py_INCREF(Py_None);
  __pyx_v_py_digest = Py_None; Py_INCREF(Py_None);
  __pyx_v_i = Py_None; Py_INCREF(Py_None);
  return;
  __Pyx_AddTraceback("_Users_robert__sage_sage_notebook_worksheets_admin_74_code_sage12_spyx_0");
}
 7:     cdef int hex_digits, i0, i1, i2, i3, i4, i5
 8:     cdef char *ab, *digest, *trial, *suffix
 9:     suffix = '******'
  __pyx_v_suffix = __pyx_k_1;
 10:     ab = alphabetcoerce
  __pyx_1 = __Pyx_GetName(__pyx_i, __pyx_n_alphabet); if (unlikely(!__pyx_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; goto __pyx_L1;}
  __pyx_2 = PyString_AsString(__pyx_1); if (unlikely((!__pyx_2) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; goto __pyx_L1;}
  Py_DECREF(__pyx_1); __pyx_1 = 0;
  __pyx_v_ab = __pyx_2;
 11:     hex_digits = intcoerce(ceil(bitscoerce/4.))
  __pyx_1 = __Pyx_GetName(__pyx_i, __pyx_n_ceil); if (unlikely(!__pyx_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; goto __pyx_L1;}
  __pyx_3 = PyFloat_FromDouble((__pyx_v_bits / 4.)); if (unlikely(!__pyx_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; goto __pyx_L1;}
  __pyx_4 = PyTuple_New(1); if (unlikely(!__pyx_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; goto __pyx_L1;}
  PyTuple_SET_ITEM(__pyx_4, 0, __pyx_3);
  __pyx_3 = 0;
  __pyx_3 = PyObject_Call(__pyx_1, __pyx_4, NULL); if (unlikely(!__pyx_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; goto __pyx_L1;}
  Py_DECREF(__pyx_1); __pyx_1 = 0;
  Py_DECREF(__pyx_4); __pyx_4 = 0;
  __pyx_1 = PyTuple_New(1); if (unlikely(!__pyx_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; goto __pyx_L1;}
  PyTuple_SET_ITEM(__pyx_1, 0, __pyx_3);
  __pyx_3 = 0;
  __pyx_4 = PyObject_Call(((PyObject*)&PyInt_Type), __pyx_1, NULL); if (unlikely(!__pyx_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; goto __pyx_L1;}
  Py_DECREF(__pyx_1); __pyx_1 = 0;
  __pyx_5 = __pyx_PyInt_int(__pyx_4); if (unlikely((__pyx_5 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; goto __pyx_L1;}
  Py_DECREF(__pyx_4); __pyx_4 = 0;
  __pyx_v_hex_digits = __pyx_5;
 12:     hash = sha
  __pyx_3 = __Pyx_GetName(__pyx_i, __pyx_n_sha); if (unlikely(!__pyx_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 12; goto __pyx_L1;}
  Py_DECREF(__pyx_v_hash);
  __pyx_v_hash = __pyx_3;
  __pyx_3 = 0;
 13:     for i0 from 0 <= i0 < 55:
  for (__pyx_v_i0 = 0; __pyx_v_i0 < 55; __pyx_v_i0++) {
 14:         suffix[0] = ab[i0]
    (__pyx_v_suffix[0]) = (__pyx_v_ab[__pyx_v_i0]);
 15:         for i1 from 0 <= i1 < 55:
    for (__pyx_v_i1 = 0; __pyx_v_i1 < 55; __pyx_v_i1++) {
 16:             suffix[1] = ab[i1]
      (__pyx_v_suffix[1]) = (__pyx_v_ab[__pyx_v_i1]);
 17:             for i2 from 0 <= i2 < 55:
      for (__pyx_v_i2 = 0; __pyx_v_i2 < 55; __pyx_v_i2++) {
 18:                 suffix[2] = ab[i2]
        (__pyx_v_suffix[2]) = (__pyx_v_ab[__pyx_v_i2]);
 19:                 for i3 from 0 <= i3 < 55:
        for (__pyx_v_i3 = 0; __pyx_v_i3 < 55; __pyx_v_i3++) {
 20:                     suffix[3] = ab[i3]
          (__pyx_v_suffix[3]) = (__pyx_v_ab[__pyx_v_i3]);
 21:                     for i4 from 0 <= i4 < 55:
          for (__pyx_v_i4 = 0; __pyx_v_i4 < 55; __pyx_v_i4++) {
 22:                         suffix[4] = ab[i4]
            (__pyx_v_suffix[4]) = (__pyx_v_ab[__pyx_v_i4]);
 23:                         for i5 from 0 <= i5 < 55:
            for (__pyx_v_i5 = 0; __pyx_v_i5 < 55; __pyx_v_i5++) {
 24:                             suffix[5] = ab[i5]
              (__pyx_v_suffix[5]) = (__pyx_v_ab[__pyx_v_i5]);
 25:                             py_digest = hash(challenge+suffix).hexdigest()
 26:                             digest = py_digestcoerce
              __pyx_2 = PyString_AsString(__pyx_v_py_digest); if (unlikely((!__pyx_2) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 26; goto __pyx_L1;}
              __pyx_v_digest = __pyx_2;
 27:                             for i from 0 <= i < hex_digits:
              for (__pyx_6 = 0; __pyx_6 < __pyx_v_hex_digits; __pyx_6++) {
                __pyx_4 = PyInt_FromLong(__pyx_6); if (unlikely(!__pyx_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 27; goto __pyx_L1;}
                Py_DECREF(__pyx_v_i);
                __pyx_v_i = __pyx_4;
                __pyx_4 = 0;
 28:                                 if digest[icoerce] != c'0': break
 29:                             else:
 30:                                 return suffixcoerce
                __pyx_3 = PyString_FromString(__pyx_v_suffix); if (unlikely(!__pyx_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 30; goto __pyx_L1;}
                __pyx_r = __pyx_3;
                __pyx_3 = 0;
                goto __pyx_L0;
              }
              __pyx_L15:;
            }
          }
        }
      }
    }
  }

  __pyx_r = Py_None; Py_INCREF(Py_None);
  goto __pyx_L0;
  __pyx_L1:;
  Py_XDECREF(__pyx_1);
  Py_XDECREF(__pyx_3);
  Py_XDECREF(__pyx_4);
  __Pyx_AddTraceback("_Users_robert__sage_sage_notebook_worksheets_admin_74_code_sage12_spyx_0._mint");
  __pyx_r = 0;
  __pyx_L0:;
  Py_DECREF(__pyx_v_hash);
  Py_DECREF(__pyx_v_py_digest);
  Py_DECREF(__pyx_v_i);
  return __pyx_r;
}


_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to