Basic Pyrex question

"Rick Muller" <[email protected]> Fri, 16 May 2008 10:12:43 -0600
Newsgroups gmane.comp.python.pyrex
Message-ID <[email protected]>
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
[email protected]

_______________________________________________
Pyrex mailing list
[email protected]
http://lists.copyleft.no/mailman/listinfo/pyrex