This shouldn't really come as a surprise.  From the Cython documentation,

"This is about 20 times slower, but still about 10 times faster than
the original Python-only integration code. This shows how large the
speed-ups can easily be when whole loops are moved from Python code
into a Cython module."

http://docs.cython.org/src/tutorial/cdef_classes.html


On Tue, Apr 9, 2013 at 12:58 PM, Nils Bruin <nbr...@sfu.ca> wrote:
> 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.
>
>

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