[Cython] ctypes Vs Cython
Sebastien Binet
seb.binet at gmail.com
Wed May 13 10:17:07 CEST 2009
hi,
On Wednesday 13 May 2009 08:35:27 Robert Bradshaw wrote:
> On May 12, 2009, at 9:47 PM, Mohamed Lrhazi wrote:
> > On Wed, May 13, 2009 at 12:31 AM, Chris Colbert
> >
> > <sccolbert at gmail.com> wrote:
> >> If your making lots of rapid calls to short running functions in the
> >> C-library, then you may start to feel the ctypes overhead.
> >
> > That's what I was afraid to hear..
>
> Hopefully after using Cython a bit, you're fears will quickly go
> away :).
>
> > What I was hoping to hear is "Oh
> > no, ctypes is all C anyways, and will perform just the same as Cython"
>
> A simple benchmark:
>
> import ctypes
> libm = ctypes.cdll.LoadLibrary("libm.dylib") # platform dependent...
> def ctypes_sum(N):
> lib_sqrt = libm.sqrt
> lib_sqrt.argtypes = (ctypes.c_double,)
> lib_sqrt.restype = ctypes.c_double
> s = 0
> for i in range(N):
> s += lib_sqrt(i)
> return s
>
> %cython
> cdef extern from "math.h":
> double sqrt(double)
>
> def cython_sum(long N):
> cdef int i
> cdef double s=0
> for i in range(N):
> s += sqrt(i)
> return s
>
> >>> time ctypes_sum(10**6)
>
> 666666166.4588418
> Time: CPU 1.13 s, Wall: 1.14 s
>
> time cython_sum(10**6)
> 666666166.4588418
> Time: CPU 0.03 s, Wall: 0.03 s
interesting simple minded benchmark :)
how would this translate into the pure-python mode ? (I couldn't seem to be
able to declare the C-sqrt function using the pure-python mode of cython:
http://wiki.cython.org/pure wasn't helpful)
cheers,
sebastien.
--
#########################################
# Dr. Sebastien Binet
# Laboratoire de l'Accelerateur Lineaire
# Universite Paris-Sud XI
# Batiment 200
# 91898 Orsay
#########################################
More information about the Cython-dev
mailing list