On May 13, 2009, at 1:50 AM, Sebastien Binet wrote:

> On Wednesday 13 May 2009 10:41:43 Robert Bradshaw wrote:
>> On May 13, 2009, at 1:17 AM, Sebastien Binet wrote:
>>> 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
>>>>>
>>>>> <[email protected]> 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 ?
>>
>> Pure Python:
>>
>> def python_sum(N):
>>      from math import sqrt
>>      s = 0
>>      for i in range(N):
>>          s += sqrt(i)
>>      return s
>>
>>>>> time python_sum(10**6)
>>
>> 666666166.4588418
>> Time: CPU 0.47 s, Wall: 0.47 s
>>
>> So for such a tiny call, ctypes is slower. (Since math.sqrt is a
>> static wrapper around libm's sqrt, no surprise here.)
>>
>>> (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)
>>
>> The pure-python mode should be exactly the same as the above, but
>> there's no way to declare functions in it (yet).
> but if you pyximport import it, you should get the performance back  
> (assuming
> the function declaration were here), right ?

Yes, that's right.

There'd have to be some way of specifying "here's a C function, but  
in pure python mode, use this python function instead."

- Robert

_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to