Augmented assignment

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Suresh Jeevanandam

    #1

    Augmented assignment

    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
  • Alex Martelli

    #2
    Re: Augmented assignment

    Suresh Jeevanandam <jm.suresh@gmai l.com> wrote:
    [color=blue]
    > Hi,
    > Is there any gain in performance because of augmented assignments.
    >
    > x += 1 vs x = x+1
    >
    > Or are both of them the same.[/color]

    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

    Comment

    • Terry Reedy

      #3
      Re: Augmented assignment


      "Suresh Jeevanandam" <jm.suresh@gmai l.com> wrote in message
      news:dte88n$568 $2@home.itg.ti. com...[color=blue]
      > Hi,
      > Is there any gain in performance because of augmented assignments.
      >
      > x += 1 vs x = x+1
      >
      > Or are both of them the same.[/color]

      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



      Comment

      • Suresh Jeevanandam

        #4
        Re: Augmented assignment

        Thanks Alex. I was not aware of mtimeit.

        regards,
        Suresh
        Alex Martelli wrote:[color=blue]
        > Suresh Jeevanandam <jm.suresh@gmai l.com> wrote:
        >[color=green]
        >> Hi,
        >> Is there any gain in performance because of augmented assignments.
        >>
        >> x += 1 vs x = x+1
        >>
        >> Or are both of them the same.[/color]
        >
        > 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[/color]

        Comment

        • bonono@gmail.com

          #5
          Re: Augmented assignment


          Terry Reedy wrote:[color=blue]
          > 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.[/color]

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

          Comment

          • Andrea Griffini

            #6
            Re: Augmented assignment

            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...
            [color=blue][color=green][color=darkred]
            >>> s = []
            >>> t = s
            >>> t = t + [1]
            >>> t[/color][/color][/color]
            [1][color=blue][color=green][color=darkred]
            >>> s[/color][/color][/color]
            [][color=blue][color=green][color=darkred]
            >>> s2 = []
            >>> t2 = s2
            >>> t2 += [1]
            >>> t2[/color][/color][/color]
            [1][color=blue][color=green][color=darkred]
            >>> s2[/color][/color][/color]
            [1][color=blue][color=green][color=darkred]
            >>>[/color][/color][/color]

            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.

            Comment

            • Terry Reedy

              #7
              Re: Augmented assignment


              <bonono@gmail.c om> wrote in message
              news:1140506094 .387829.60890@g 14g2000cwa.goog legroups.com...[color=blue]
              >
              > Terry Reedy wrote:[color=green]
              >> 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.[/color]
              >
              > Would x=x + 1 be more readable, regardless of the background(whet her
              > being introduced to the += form in some other language like C before) ?[/color]

              To *me*,

              able.baker.char les.delta += 1

              is more easily read than

              able.baker.char les.delta = able.baker.char les.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.char les.delta = able.baker.char ley.delta +1

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

              Terry Jan Reedy



              Comment

              • Terry Hancock

                #8
                Re: Augmented assignment

                On Tue, 21 Feb 2006 10:55:42 +0530
                Suresh Jeevanandam <jm.suresh@gmai l.com> wrote:[color=blue]
                > Is there any gain in performance because of
                > augmented assignments.
                >
                > x += 1 vs x = x+1[/color]

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

                length_of_objec t_I_must_descri be_very_careful ly += 1

                vs

                length_of_objec t_I_must_descri be_very_careful ly = length_of_objec t_I_must_descri be_very_careful ly + 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 (hancock@Anansi Spaceworks.com)
                Anansi Spaceworks http://www.AnansiSpaceworks.com

                Comment

                • gene tani

                  #9
                  Re: Augmented assignment


                  Terry Hancock wrote:[color=blue]
                  > On Tue, 21 Feb 2006 10:55:42 +0530
                  > Suresh Jeevanandam <jm.suresh@gmai l.com> wrote:[/color]
                  [color=blue]
                  > Seriously,
                  > I think they are usually equivalent internally,
                  > at least for immutable objects.
                  >[/color]

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



                  Comment

                  Working...