Pyrex - Numeric demo doesn't build in 0.7.2

Fernando Perez fperez528 at yahoo.com
Wed May 14 00:52:24 EDT 2003


Greg Ewing (using news.cis.dfn.de) wrote:

> Are you sure you're using the right version of
> Pyrex to compile it? The version of numeric_demo.pyx
> included with 0.7.2 uses an experimental feature that
> I haven't documented yet, and you won't be able to
> compile it with anything earlier than 0.7.2.

Ah, sorry about that.  I didn't realize I did have an old version around, and I
was convinced I'd recently upgraded. Oh well, thanks a lot for the quick
response.

I'm putting together a talk for the FrontRange Pythoneers group next week on
using scipy.weave to inline C/C++ for speed, and I'd like to put in a pyrex
comparison.

My pyrex code is currently very crude, but I was surprised to see only a 3%
improvement over the raw python version.  I'm curious if you might have some
tips on what I'm doing wrong, if anything.

I was, of course, planning on showing my results with the proper caveat that I
know _nothing_ about pyrex, so that any poor performance shouldn't be construed
as pyrex's fault.  But still, if I can improve things for this comparison, I'd
like to show pyrex's best side :)

The python code I'm running is:


import math,cmath
import LinearAlgebra
from Numeric import *

def quantum_cat_python(N,kappa):
    """For a given N and kappa this function returns
    the corresponding unitary matrix U 
    of the quantized perturbed cat map.
    
    MODIFIED VERSION, to optimize the python loop a bit and make the
    comparison with the numeric and weave versions fairer.  """
    
    from math import sin,sqrt  # To avoid using the Numeric ones

    # First initialize complex matrix with NxN elements
    mat = zeros((N,N), Complex)
    # precompute a few things outside the loop
    sqrt_N_inv = 1.0/sqrt(N)
    alpha = 2.0*pi/N
    kap_al = kappa/alpha
    
    # now we fill each element
    rng = range(0,N)
    exp = cmath.exp
    for k in rng:
        for l in rng:
            mat[k,l] = sqrt_N_inv * \
                       exp(1j*(alpha*(k*k-k*l+l*l) + \
                               kap_al*sin(alpha*l)))
    return mat

And here is my corresponding pyrex version:

import cmath
from Numeric import zeros,Complex
from math import sin,pi,sqrt

def quantum_cat_pyrex(int N,float kappa):

    # type declarations
    cdef int k,l
    cdef double alpha, kap_al,sqrt_N_inv

    # First initialize complex matrix with NxN elements
    mat = zeros((N,N), Complex)
    # precompute a few things outside the loop
    sqrt_N_inv = 1.0/sqrt(N)
    alpha = 2.0*pi/N
    kap_al = kappa/alpha
    
    # now we fill each element
    exp = cmath.exp
    I = complex(0,1)
    for k from 0 <= k < N:
        for l from  0 <= l < N:
            mat[k,l] = sqrt_N_inv * \
                       exp(I*(alpha*(k*k-k*l+l*l) + kap_al*sin(alpha*l)))
    return(mat)


My timing results, along with a few variations of inlined C/C++, are the
following (the last two columns show the ratios of each time to the fastest and
to python's time):

In [4]: time_all()
Timing tests executed with N=1000, kappa=0.3

Version Time    To fast To Python
=================================
C++      0.48    1.00   53.93
C++ 2    0.48    1.00   53.71
C++ 3    0.49    1.03   52.22
C++ 4    0.67    1.41   38.30
C        1.29    2.69   20.05
numeric  1.97    4.12   13.08
pyrex   24.99   52.22    1.03
python  25.80   53.93    1.00

I'd love to hear of any changes I should make to the pyrex version to improve
things.  That's why I was trying to run the numeric_demo file, looking for
improvements in handling Numeric arrays.

best regards,

Fernando.




More information about the Python-list mailing list