Augmented assignment

S

Suresh Jeevanandam

Hi,
Is there any gain in performance because of augmented assignments.

x += 1 vs x = x+1

Or are both of them the same.

regards,
Suresh
 
A

Alex Martelli

Suresh Jeevanandam said:
Hi,
Is there any gain in performance because of augmented assignments.

x += 1 vs x = x+1

Or are both of them the same.

Just *MEASURE*, man!

helen:~/apy alex$ python -mtimeit -s'x=0.0' 'x=x+1'
1000000 loops, best of 3: 0.507 usec per loop

helen:~/apy alex$ python -mtimeit -s'x=0.0' 'x+=1'
1000000 loops, best of 3: 0.504 usec per loop

Basically a dead draw, so use what's clearest to you. And learn to use
-mtimeit to satisfy most such curiosities much more effectively.


Alex
 
T

Terry Reedy

Suresh Jeevanandam said:
Hi,
Is there any gain in performance because of augmented assignments.

x += 1 vs x = x+1

Or are both of them the same.

The main gain is in programmer performance for writing a long name such as
number_of_items once instead of twice. Also in reading to see that the
same name gets rebound to the new name.

Program performance might be noticeable if 'x' is something like a.b.c.d
that takes some lookup time. But again, I would use the += form for
readability without testing run time.

Terry Jan Reedy
 
B

bonono

Terry said:
Program performance might be noticeable if 'x' is something like a.b.c.d
that takes some lookup time. But again, I would use the += form for
readability without testing run time.

Would x=x + 1 be more readable, regardless of the background(whether
being introduced to the += form in some other language like C before) ?
 
A

Andrea Griffini

I think it heavily depends on what is "x". If x is bound to a mutable
x=x+1 and x+=1 can not only have different speed but indeed can do two
very unrelate things (the former probably binding to a new object, the
latter probably modifying the same object). For example consider what
happens with lists and [1] instead of 1...
s = []
t = s
t = t + [1]
t [1]
s []
s2 = []
t2 = s2
t2 += [1]
t2 [1]
s2 [1]

Also if x is not a single name but a more convoluted expression with +=
that expression is evaluated once and even in this case there can be
differences in speed and not only in speed.
 
T

Terry Reedy

Would x=x + 1 be more readable, regardless of the background(whether
being introduced to the += form in some other language like C before) ?

To *me*,

able.baker.charles.delta += 1

is more easily read than

able.baker.charles.delta = able.baker.charles.delta +1

because it is clear that there is one and only one attribute involved,
being updated in place, whereas the latter might have been

able.baker.charles.delta = able.baker.charley.delta +1

Therefore, as I said originally *I* would use the += form.

Terry Jan Reedy
 
T

Terry Hancock

Is there any gain in performance because of
augmented assignments.

x += 1 vs x = x+1

Yep. I perform better when I only type names once.
Especially if they are long:

length_of_object_I_must_describe_very_carefully += 1

vs

length_of_object_I_must_describe_very_carefully = length_of_object_I_must_describe_very_carefully + 1

Oh, you mean performance of the computer? ;-)

But Python is all about optimizing the
performance of programmers, not computers!

Seriously,
I think they are usually equivalent internally,
at least for immutable objects.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top