-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix julialang.org fib example for Cython #21
Comments
@eteq Thanks for pointing this out. This being said, I’ve rerun the recursive benchmarks and I indeed see a performance gain. However, for fibonacci it’s relatively far from the 20-fold performance gain you mentioned. For the quicksort it’s only a few percent. |
@cosmo-ethz - A fair question why it isn't the default. Maybe backwards-compatibility? Or perhaps there's some subtle corner case where you don't want the hybrid behavior? Someone on cython-devel would probably know. Hmm, I just ran it now locally on OS X 10.9 (I was using ubuntu before), and saw this:
Where On A little off-topic, but some of the other examples are sort of mis-using Cython, which is why it sometimes comes in similar to regular python. I found most of these things using the |
@eteq i’ve rerun the benchmark again but I don’t see the same speedup as you do. Which version of Python and Cython are you using for your tests? Ok, thanks. I have to look into that. |
@cosmo-ethz - I was using py2.7 for both the Ubuntu (~20x speedup) and OS X (~15x speedup) cases. On OS X I have Cython 0.21 (the latest), and I think Ubuntu is 0.20? (I don't have access to that machine right now). It's whatever version is the in the Ubuntu 14.04 package manager, anyway. |
@cosmo-ethz - One possible problem I encountered while testing this: oddness occurs if you use the same function names for both in an ipython notebook. I think what was happening was that it compiled the
|
For what it's worth, I also did not know about the hybrid function declaration but was aware of the fact that I would loose time by calling python functions and not cython ones. Therefore I had been solving these problems by making a wrapper to the real function, for example:
Timing wise I get:
|
@nparley That's a nice work around. I've just pushed a new version of the benchmarks, where we adapted the Cython code |
@cosmo-ethz I am not sure if you edited the sort as well but this is my quick cython edit (pretty much what @eteq said above)
The times here:
Hope looks a really nice piece of work :) |
@nparley - yep, that does the trick too. While the first case (the fibonacci one) is just as fast if you just substitute |
Just to be clear, though, @cosmo-ethz, cython's docs clearly advocate the use of |
@eteq yes I agree that cpdef is cleaner. I guess the naming convention makes sense too:
|
@nparley thanks for putting together this example how to pass around the array buffers with cython. The impact on the performance is quite impressive. (The function signature becomes somewhat ... interesting 👍 ) However, I think its clear that with sufficient time, expertise and tinkering it is possible to improve the Cython-code performance to C-level code. From a certain point on, though, the code resembles C code very much. So in that case it’s questionable if not going to C directly would be simpler. I guess this depends on the taste and skills in the different languages. |
@cosmo-ethz - I'm with you on the idea that hope is clearly the clearest (in the sense of "more python like"). But I think just expecting Cython to behave like hope well in this way i somewhat misrepresentings what Cython is for. So I think your point is made just as well by using Cython the way it is meant to be used (optimized and thus complex), while making it clear that this tools all have uses depending on your preferences/code situation. |
I agree. I think the comparisons should be done with the different versions optimised. A lot of people are not going to want to learn how to use cython and hope and numbra as great tools here. Of course cython has openmp support which is really useful when you have access to multi core machines to do your number crunching. |
Right now, the fibbonachi example (visible at http://nbviewer.ipython.org/url/refreweb.phys.ethz.ch/hope/notebooks/julialang.org.ipynb ... for some reason this doesn't match this repository?) reads:
This is a mis-use of Cython, however. If you change it instead to
That yields a drastic speedup, making the Cython example equivalent to native code (and hope).
The point is that using "def" in tight loops is a mis-use of Cython: it makes Cython functions act just like regular Python functions, rather than using it as a c function with an int return type, which is what makes the recursion here go fast.
The text was updated successfully, but these errors were encountered: