Augmented assignment

Discussion in 'Python' started by Suresh Jeevanandam, Feb 21, 2006.

  1. 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
    Suresh Jeevanandam, Feb 21, 2006
    #1
    1. Advertising

  2. Suresh Jeevanandam <> wrote:

    > 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
    Alex Martelli, Feb 21, 2006
    #2
    1. Advertising

  3. Thanks Alex. I was not aware of mtimeit.

    regards,
    Suresh
    Alex Martelli wrote:
    > Suresh Jeevanandam <> wrote:
    >
    >> 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
    Suresh Jeevanandam, Feb 21, 2006
    #3
  4. Suresh Jeevanandam

    Terry Reedy Guest

    "Suresh Jeevanandam" <> wrote in message
    news:dte88n$568$...
    > 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
    Terry Reedy, Feb 21, 2006
    #4
  5. Suresh Jeevanandam

    Guest

    Terry Reedy wrote:
    > 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) ?
    , Feb 21, 2006
    #5
  6. 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.
    Andrea Griffini, Feb 21, 2006
    #6
  7. Suresh Jeevanandam

    Terry Reedy Guest

    <> wrote in message
    news:...
    >
    > Terry Reedy wrote:
    >> 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) ?


    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
    Terry Reedy, Feb 21, 2006
    #7
  8. Suresh Jeevanandam

    gene tani Guest

    Terry Hancock wrote:
    > On Tue, 21 Feb 2006 10:55:42 +0530
    > Suresh Jeevanandam <> wrote:


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


    yah, but when you do augmented assigns on lists, or mix immutable an
    dmutable:

    http://zephyrfalcon.org/labs/python_pitfalls.html
    gene tani, Feb 21, 2006
    #8
  9. On Tue, 21 Feb 2006 10:55:42 +0530
    Suresh Jeevanandam <> wrote:
    > 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.

    --
    Terry Hancock ()
    Anansi Spaceworks http://www.AnansiSpaceworks.com
    Terry Hancock, Feb 21, 2006
    #9
    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. Doug Tolton

    Augmented Assignment question

    Doug Tolton, Jul 16, 2003, in forum: Python
    Replies:
    6
    Views:
    466
    John Machin
    Jul 19, 2003
  2. Delaney, Timothy C (Timothy)

    RE: Augmented Assignment question

    Delaney, Timothy C (Timothy), Jul 17, 2003, in forum: Python
    Replies:
    0
    Views:
    372
    Delaney, Timothy C (Timothy)
    Jul 17, 2003
  3. Pierre Barbier de Reuille

    User-defined augmented assignment

    Pierre Barbier de Reuille, Sep 29, 2005, in forum: Python
    Replies:
    4
    Views:
    298
    Tom Anderson
    Oct 2, 2005
  4. Josiah Carlson
    Replies:
    4
    Views:
    343
    Boris Borcic
    Jun 19, 2006
  5. Tim N. van der Leeuw

    Nested scopes, and augmented assignment

    Tim N. van der Leeuw, Jul 4, 2006, in forum: Python
    Replies:
    39
    Views:
    775
    Piet van Oostrum
    Jul 10, 2006
Loading...

Share This Page