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


Groups > comp.lang.python > #61463

Re: squeeze out some performance

From Robin Becker <robin@reportlab.com>
Subject Re: squeeze out some performance
Date 2013-12-10 12:54 +0000
References <ce6477c1-29c0-43b3-9e56-336a5825869b@googlegroups.com> <9df6ccd7-828d-43be-ac49-fe1c6a38bae7@googlegroups.com> <CAPM-O+wyt_7VyO+yFO9eVDUnqJAxr5fa9rAONQHCbFgzkbJdhw@mail.gmail.com> <52A5E7BC.5090600@chamonix.reportlab.co.uk> <almarsoft.2469747325533878265@news.gmane.org>
Newsgroups comp.lang.python
Message-ID <mailman.3823.1386680060.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 09/12/2013 20:46, Dave Angel wrote:
> On Mon, 09 Dec 2013 15:54:36 +0000, Robin Becker <robin@reportlab.com> wrote:
>> On 06/12/2013 22:07, Joel Goldstick wrote:
>> >      end, start = start, end
>
>> a similar behaviour for simple assignments
>
>> for less than 4 variables the tuple method is faster.
>
> What does speed have to do with it?  When you want to swap two variables,  the
> tuple assignment reads better.
>
Well the OP is asking about performance so I guess the look and feel might be 
sacrificed for speed in some circumstances.

The tuple approach is more appealing when the lhs & rhs are connected, but it 
seems that even for more general assignments the tuple assignment may be faster 
for small numbers of variables. Looking at the output of dis for this case

d,e,f=c,b,a

it seems that we get code like this
LOAD_NAME                3 (c)
LOAD_NAME                2 (b)
LOAD_NAME                1 (a)
ROT_THREE
ROT_TWO
STORE_NAME               4 (d)
STORE_NAME               5 (e)
STORE_NAME               6 (f)



for

d = c
e = b
f = a

we get this
LOAD_NAME                3 (c)
STORE_NAME               4 (d)
LOAD_NAME                2 (b)
STORE_NAME               5 (e)
LOAD_NAME                1 (a)
STORE_NAME               6 (f)

which is not obviously slower, but I consistently get the former to be faster. I 
suppose it's a cache issue or something. However, for this particular case when 
the variables are not connected I find the tuple assignment less pythonic, but 
perhaps faster (even though in this case no tuples are built).
-- 
Robin Becker

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


Thread

squeeze out some performance Robert Voigtländer <r.voigtlaender@gmail.com> - 2013-12-06 00:47 -0800
  Re: squeeze out some performance Jeremy Sanders <jeremy@jeremysanders.net> - 2013-12-06 10:46 +0100
  Re: squeeze out some performance Chris Angelico <rosuav@gmail.com> - 2013-12-06 23:13 +1100
    Re: squeeze out some performance Robert Voigtländer <r.voigtlaender@gmail.com> - 2013-12-06 08:29 -0800
      Re: squeeze out some performance Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-12-06 16:36 +0000
        Re: squeeze out some performance Robert Voigtländer <r.voigtlaender@gmail.com> - 2013-12-06 08:44 -0800
  Re: squeeze out some performance John Ladasky <john_ladasky@sbcglobal.net> - 2013-12-06 08:52 -0800
    Re: squeeze out some performance Joel Goldstick <joel.goldstick@gmail.com> - 2013-12-06 17:07 -0500
    Re: squeeze out some performance Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-12-06 22:38 +0000
    Re: squeeze out some performance Dan Stromberg <drsalists@gmail.com> - 2013-12-06 15:01 -0800
      Re: squeeze out some performance Robert Voigtländer <r.voigtlaender@gmail.com> - 2013-12-09 06:19 -0800
        Re: squeeze out some performance Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-12-09 14:52 +0000
    Re: squeeze out some performance Robin Becker <robin@reportlab.com> - 2013-12-09 15:54 +0000
    Re: squeeze out some performance Dave Angel <davea@davea.name> - 2013-12-09 15:46 -0500
    Re: squeeze out some performance Robin Becker <robin@reportlab.com> - 2013-12-10 12:54 +0000
  Re: squeeze out some performance Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-12-09 23:57 +0000
    Re: squeeze out some performance Robert Voigtländer <r.voigtlaender@gmail.com> - 2013-12-10 04:14 -0800

csiph-web