Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #40048 > unrolled thread

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

Started byChris Angelico <rosuav@gmail.com>
First post2013-02-27 21:49 +1100
Last post2013-02-28 07:06 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: python 3 problem: how to convert an extension method into a class Method Chris Angelico <rosuav@gmail.com> - 2013-02-27 21:49 +1100
    Re: python 3 problem: how to convert an extension method into a class Method Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-28 07:06 +0000

#40048 — Re: python 3 problem: how to convert an extension method into a class Method

FromChris Angelico <rosuav@gmail.com>
Date2013-02-27 21:49 +1100
SubjectRe: python 3 problem: how to convert an extension method into a class Method
Message-ID<mailman.2605.1361962147.2939.python-list@python.org>
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

[toc] | [next] | [standalone]


#40118

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-02-28 07:06 +0000
Message-ID<512f01de$0$30001$c3e8da3$5496439d@news.astraweb.com>
In reply to#40048
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web