Robert Bradshaw wrote:
> On Mar 4, 2008, at 10:21 PM, Carlo Hamalainen wrote:
>> I have this request for Cython benchmarks on a blog post of mine - is
>> there any data out there, specifically comparing Cython to C/C++?
>>
>> ---------- Forwarded message ----------
>> Does somebody else have some experience in how cython compares
>> with C/C++? Every once in a while I need to do some computation
>> (something NP-complete or worse in general, so it
>> usually ends up as an ugly backtracking). I'd be happy to do
>> everything from within Sage (and using python/cython), but I'm not
>> sure, if it is fast enough (or if it getting fast enough, I suppose
>> that cython is improving gradually).
>
> That all depends on how it is written. If you are using a lot of
> python data types, it will be slower. If not, you can write Cython
> that translates to nearly pure C (and runs at that speed).
I think the best way to do this is to let Cython generate the C code and
then look through the time critical parts to check for obvious problems,
such as multiple back and forth conversion between C types and Python
types or integer loops that run with Python run variables. Usually, this
can be fixed by a couple of "cdef" declarations in the Cython code, which
then translates to more optimal C code.
I don't think a general benchmark is of much value here, as it would not
catch the most common problems like the ones above.
And if you want to optimise, there is no better way than timing your own
code, which the timeit module in Python's stdlib makes easy enough to do.
Just write a Cython module with a Python ("def") function that does
exactly what you want to benchmark, and then call it through timeit.
That's a one-liner.
I added a quick blog entry on this (currently untested, but I'll fix that
as soon as I get to it). You should get the idea anyway.
http://behnel.de/cgi-bin/weblog_basic/index.php?p=17
Stefan
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev