Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Also, cpdef methods do have a little overhead because they check whether they have been overridden, even when called as cdef method. If your instance has an instance dictionary, the overhead is significant compared to normal c function calling overhead:

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)')

results in

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

The dictionary present on b (by subclassing) significantly slows down code that seems to run entirely at C level.