Jelle Feringa wrote:
>> I would call it doomed by design. If you use it from Python, I don't  
>> think
>> you can beat direct iteration through __getitem__ *of the object*  
>> that you
>> iterate over by introducing another level of indirection that  
>> introduces a
>> method call. I tried islice() and it's still a bit faster than the  
>> class
>> you presented.
>>     
>
> Given that islice does the same thing better definitely doomed ;')
>
>   
>> Maybe you could give us some more insight into your actual use case,  
>> that
>> might allow us to come up with better alternatives.
>>     
>
> Thanks, that's very kind.
> Thing is that currently I'm working on sampling b-spline surfaces, and  
> compute the curvature of such a surface.
> Basically that comes down to making a 500*500 grid of curvature samples.
> Computing the curvature is really quick ( call to a SWIG wrapped  
> object ).
> My code looks like:
>
> for a in xrange(1, 500):
>       for b in xrange(1, 500):
>               do_curvature()
>
> When I place pass instead of do_curvature() I notice that the time for  
> such a loop is basically the same, which is why I'm interested in  
> moving the for loops to cython.
> This is a pattern that is happens often in my code, hence it would be  
> lovely to make a function something like:
>
> loop_fast2d( do_curvature, (1,500), (1,500) )
>
> What makes this pattern hard is that you would have to loop again  
> through the results, which reduces the time saved by a fast  
> loop_fast2d function by half.
> So perhaps this pattern is more effective:
>
> loop_fast2d( do_curvature, do_result, (1,500), (1,500) )
>
> What do you think? Is this pattern generic enough to be sped up in  
> Cython?
>   
You should read through the recent thread "Comments on example code" 
which touches on the same issue.

As Robert said our definitions of "quick" likely vary a great deal. This 
is how to get quick code though: You need to drop the SWIG wrapper and 
instead wrap the code using Cython. OTOH, that will likely give between 
50x to 1000x speedup depending on the usecase.

Basically you need to make sure that "do_curvature" and "do_result" can 
be called in a fast way. One way of doing that is

cdef class Float2DGridFunc:
    cdef float evaluate(int i, int j): pass

cdef extern from "yourheader.h":
    # define C function you are calling the Cython way

cdef class DoCurvature(Float2DGridFunc):
    cdef float evaluate(int i, int j): return 
direct_call_to_c_function(i, j)

And keep the implementation of DoCurvature in Cython. Then you will be 
able to do this in plain Python:
import your_cython_mod
your_cython_mode.loop_fast_2d(your_cython_mode.DoCurvature(), ...)

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

Reply via email to