[Pyrex] Basic Pyrex question

Rick Muller rpmuller at gmail.com
Fri May 16 18:12:43 CEST 2008


I'm just learning Pyrex, and am making a mistake somewhere. I wanted to play
with the simple function:

def fastsumpairs(l):
    s = 0
    for i in xrange(len(l)):
        for j in xrange(i):
            s += l[i]*l[j]
    return s

The first time through, the python and pyrex versions of this routine gave
the same result, and the pyrex version was about 30% faster. Sweet.

timeit(sumpairs(range(1,1000)): 0.202557 seconds
124583708250
timeit(fastsumpairs(range(1,1000)): 0.155977 seconds
124583708250

Now I tried to modify the code to tell it that s,i,j were ints:

def fastsumpairs(l):
    cdef int s, i, j
    s = 0
    for i in xrange(len(l)):
        for j in xrange(i):
            s += l[i]*l[j]
    return s


timeit(sumpairs(range(1,1000)): 0.202557 seconds
124583708250
timeit(fastsumpairs(range(1,1000)): 0.069682 seconds
29656666

So the function is now much faster, but it gives the wrong results. Any
hints as to why?

-- 
Rick Muller
rpmuller at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.copyleft.no/pipermail/pyrex/attachments/20080516/558a7451/attachment.html 


More information about the Pyrex mailing list