squeeze out some performance

Robin Becker robin at reportlab.com
Tue Dec 10 13:54:13 CET 2013


On 09/12/2013 20:46, Dave Angel wrote:
> On Mon, 09 Dec 2013 15:54:36 +0000, Robin Becker <robin at 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




More information about the Python-list mailing list