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


Groups > comp.lang.python > #8341

Re: performance critical Python features

References <BANLkTimZk6C4PECy6KNrQ91fWNQG37Mgjw@mail.gmail.com> <mailman.337.1308852020.1164.python-list@python.org> <4e03d54e$0$29975$c3e8da3$5496439d@news.astraweb.com>
Date 2011-06-24 12:08 +1000
Subject Re: performance critical Python features
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.347.1308881313.1164.python-list@python.org> (permalink)

Show all headers | View raw


On Fri, Jun 24, 2011 at 10:07 AM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> On Fri, 24 Jun 2011 04:00:17 +1000, Chris Angelico wrote:
>
>> On Fri, Jun 24, 2011 at 2:58 AM, Eric Snow <ericsnowcurrently@gmail.com>
>> wrote:
>>> So, which are the other pieces of Python that really need the heavy
>>> optimization and which are those that don't?  Thanks.
>>>
>>>
>> Things that are executed once (imports, class/func definitions) and
>
> You can't assume that either of those things are executed once. Consider
> this toy example:

Sure. I was talking in generalities; of course you can do expensive
operations frequently. If you wanted to, you could do this:

radius=5
circum=0
for i in range(10,1000):
    c=radius*calculate_pi_to_n_decimals(i)
    if c>circum: circum=c

Calculates the highest possible circumference of a circle of that
radius. Does this mean we now have to optimize the pi calculation
algorithm so it can be used in a tight loop? Well, apart from the fact
that this code is moronic, no. All you need to do is cache. (Although
I guess in a way that's an optimization of the algorithm. It's the
same optimization as is done for imports.)

But generally speaking, functions are called more often than they're
defined, especially when we're talking about tight loops. And while
your example could be written without the repeated definition:

def outer(a, b):
   x=b**2 - a**2
   return (x*a - b)*(x*b - a) - 1

results = [outer(a, b) for (a, b) in coordinate_pairs()]

(at least, I think this is the same functionality), if inner() were
recursive, that would be different. But recursive inner functions
aren't nearly as common as write-once-call-many functions.

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Re: performance critical Python features Chris Angelico <rosuav@gmail.com> - 2011-06-24 04:00 +1000
  Re: performance critical Python features Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-24 00:07 +0000
    Re: performance critical Python features Chris Angelico <rosuav@gmail.com> - 2011-06-24 12:08 +1000

csiph-web