Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Re: python 3 problem: how to convert an extension method into a classMethod (http://www.velocityreviews.com/forums/t958125-re-python-3-problem-how-to-convert-an-extension-method-into-a-classmethod.html)

Chris Angelico 02-27-2013 10:49 AM

Re: python 3 problem: how to convert an extension method into a classMethod
 
On Wed, Feb 27, 2013 at 9:36 PM, Robin Becker <robin@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.

ChrisA

Steven D'Aprano 02-28-2013 07:06 AM

Re: python 3 problem: how to convert an extension method into aclassMethod
 
On Wed, 27 Feb 2013 21:49:04 +1100, Chris Angelico wrote:

> On Wed, Feb 27, 2013 at 9:36 PM, Robin Becker <robin@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


All times are GMT. The time now is 04:43 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.