Sometimes one ends up with implementing really small access routines,
just to do a lookup for something. In cython you might even want to do
that with some "cdef" methods. For general accessibility, you might
consider slapping a "cpdef" on rather than a "cdef". That may come at
a serious price, because "cpdef" methods also check if they have been
overridden on the instance or via subclassing if they get called via
their cdef interface:

with the following code we get:

cython("""
cdef class A(object):
    cpdef int a(self):
        return 0
    cdef int b(self):
        return 0
    cpdef int test_a(self, long N):
        cdef long i
        for i in xrange(N):
            self.a()
        return 0
    cpdef int test_b(self, long N):
        cdef long i
        for i in xrange(N):
            self.b()
        return 0
""")

t=A()
timeit('t.test_a(10000000)')
timeit('t.test_b(10000000)')
class B(A):
    pass

s=B()
timeit('s.test_a(10000000)')
timeit('s.test_b(10000000)')

25 loops, best of 3: 21.2 ms per loop
25 loops, best of 3: 15.9 ms per loop
5 loops, best of 3: 507 ms per loop
25 loops, best of 3: 15.9 ms per loop

As you can see for A it's not so bad, because there's no instance
dict. However, for the subclassed s there is an instance dict. Now the
cpdef method s.a (even inside the cython!) checks every time if it has
been overridden with a full attribute lookup.

Timings improve with attribute caching turned on (of course only for
s.test_a, because the other tests hardly do any attribute lookup)

25 loops, best of 3: 21.2 ms per loop
25 loops, best of 3: 15.9 ms per loop
5 loops, best of 3: 303 ms per loop
25 loops, best of 3: 16 ms per loop

but the penalty is still significant if your routine does very little
by itself. This was certainly unexpected for me. I was expecting that,
as far as cdef is concerned, the p in cpdef would have no effect.

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-devel?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to