On-topic: alternate Python implementations
Stefan Behnel
stefan_ml at behnel.de
Sun Aug 5 01:37:48 EDT 2012
Paul Rubin, 05.08.2012 03:38:
> Steven D'Aprano writes:
>> Runtime optimizations that target the common case, but fall back to
>> unoptimized code in the rare cases that the optimization doesn't apply,
>> offer the opportunity of big speedups for most code at the cost of
>> trivial slowdowns when you do something unusual.
>
> The problem is you can't always tell if the unusual case is being
> exercised without an expensive dynamic check, which in some cases must
> be repeated in every iteration of a critical inner loop, even though it
> turns out that the program never actually uses the unusual case.
Cython does a lot of optimistic optimisations. That's where a large part of
that huge C file comes from that Cython generates from even simple Python code.
For example, in CPython, C function calls are so ridiculously faster than
Python function calls that it's worth some effort if it saves you from
packing an argument tuple to call into a Python function. In fact, we've
been thinking about ways to export C signatures from Python function
objects, so that code implemented in C (or a C compatible language) can be
called directly from other code implemented in C. That's very common in the
CPython ecosystem.
There are a lot of simple things that quickly add up into a much better
performance on average.
Stefan
More information about the Python-list
mailing list