python 3 problem: how to convert an extension method into a class Method

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Feb 28 02:06:06 EST 2013


On Wed, 27 Feb 2013 21:49:04 +1100, Chris Angelico wrote:

> On Wed, Feb 27, 2013 at 9:36 PM, Robin Becker <robin at reportlab.com>
> wrote:
>> However, in my case the method takes
>>
>>
>>
>>                       py  C
>> utf8 bytes            50  20 usec
>> unicode               39  15
>>
>> here py refers to a native python method and C  to the extension method
>> after adding to the class. Both are called via an instance of the
>> class.
> 
> Which raises the obvious question: Does it even matter? Will the saving
> of a few microseconds really make a difference? Python's best feature is
> its clarity of code, not its blazing performance; its performance goal
> is "fast enough", and for many MANY purposes, you won't be able to tell
> the difference between that and "awesome". Don't sacrifice your code's
> clarity to the little tin god of efficiency until you're sure you
> actually get something back.


While this is true, I point out that we're not really talking about a 
"few" microseconds, but over 15µs per call, more than doubling the speed 
of the function call. That's not insignificant, and calls to that 
function could well be a bottleneck determining the application's total 
processing time. Robin's very first sentence in this thread states:

"In python 2 I was able to improve speed of reportlab using a C extension
to optimize some heavily used methods."

which suggests rather strongly that, yes, it does matter.

Re-writing critical sections of code in C is a well-known, recommended 
approach to speeding up Python. You will see it in the Python library, 
where there are accelerated C versions of modules such as decimal, pickle 
and bisect. There's a standard library module in CPython for calling C 
code directly from your Python code (ctypes), and at least two others 
(cffi from PyPy, and weave), at least two projects for interfacing Python 
with C or C++ (boost, swig), and three projects for writing C code using 
Python-like syntax (pyrex, cython and numba).



-- 
Steven



More information about the Python-list mailing list