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

Discussion in 'Python' started by Chris Angelico, Feb 27, 2013.

  1. On Wed, Feb 27, 2013 at 9:36 PM, Robin Becker <> 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
     
    Chris Angelico, Feb 27, 2013
    #1
    1. Advertising

  2. 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 <>
    > 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
     
    Steven D'Aprano, Feb 28, 2013
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robin Becker
    Replies:
    1
    Views:
    139
    Steven D'Aprano
    Feb 27, 2013
  2. Dave Angel
    Replies:
    0
    Views:
    134
    Dave Angel
    Feb 26, 2013
  3. Peter Otten
    Replies:
    0
    Views:
    132
    Peter Otten
    Feb 26, 2013
  4. Mark Lawrence
    Replies:
    0
    Views:
    137
    Mark Lawrence
    Feb 26, 2013
  5. Peter Otten
    Replies:
    0
    Views:
    140
    Peter Otten
    Feb 26, 2013
Loading...

Share This Page