Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Feature Forums
  3. - Uncategorised posts -
  4. Which one would be faster in Python i = i + 1 or i += 1 ?

Which one would be faster in Python i = i + 1 or i += 1 ?

Scheduled Pinned Locked Moved - Uncategorised posts -
pythonquestion
7 Posts 4 Posters 9 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member_14842425
    wrote on last edited by
    #1

    I haven't found any reliable answers on google so I am asking here. I know these two expressions produce the same results but I would like to know which one is faster and why is it faster. If I were to use them in two different loops with each one iterating about 100 times, would one loop finish earlier than the other? and by what margin?

    K 1 Reply Last reply
    0
    • M Member_14842425

      I haven't found any reliable answers on google so I am asking here. I know these two expressions produce the same results but I would like to know which one is faster and why is it faster. If I were to use them in two different loops with each one iterating about 100 times, would one loop finish earlier than the other? and by what margin?

      K Offline
      K Offline
      kalberts
      wrote on last edited by
      #2

      So why not try it out? It could vary with the implementation, and with the Python version. If you try it out for yourself, you get the right answer for your installation. I would be very surprised if there was any difference at all. There will always be some random fluctuations, due to interrupts and OS activity, but that should affect both alternatives equally. In any case: In 100 iterations, the difference would be so small it would not be possible to measure. Iterate at least a few million times before drawing any conclusion.

      M S B 3 Replies Last reply
      0
      • K kalberts

        So why not try it out? It could vary with the implementation, and with the Python version. If you try it out for yourself, you get the right answer for your installation. I would be very surprised if there was any difference at all. There will always be some random fluctuations, due to interrupts and OS activity, but that should affect both alternatives equally. In any case: In 100 iterations, the difference would be so small it would not be possible to measure. Iterate at least a few million times before drawing any conclusion.

        M Offline
        M Offline
        Member_14842425
        wrote on last edited by
        #3

        Well I don't know how to try this out for myself. I am just a beginner in python so I don't know much about measuring the time taken by a program to complete. Could you please explain how this could be done?

        K 1 Reply Last reply
        0
        • M Member_14842425

          Well I don't know how to try this out for myself. I am just a beginner in python so I don't know much about measuring the time taken by a program to complete. Could you please explain how this could be done?

          K Offline
          K Offline
          kalberts
          wrote on last edited by
          #4

          I do not code in Python on a regular basis, and if I do, I have to google a lot... So I googled "python timing", and the first hit, at performance - Measure time elapsed in Python - Stack Overflow[^] discusses a few alternatives. It looks like the best solution would be something like

          from timeit import default_timer as timer

          start = timer()

          ...

          end = timer()
          print(end - start) # Time in seconds, e.g. 5.38091952400282

          Obviously, the "# ..." indicates where you put you one-million iterations loop.

          M 1 Reply Last reply
          0
          • K kalberts

            I do not code in Python on a regular basis, and if I do, I have to google a lot... So I googled "python timing", and the first hit, at performance - Measure time elapsed in Python - Stack Overflow[^] discusses a few alternatives. It looks like the best solution would be something like

            from timeit import default_timer as timer

            start = timer()

            ...

            end = timer()
            print(end - start) # Time in seconds, e.g. 5.38091952400282

            Obviously, the "# ..." indicates where you put you one-million iterations loop.

            M Offline
            M Offline
            Member_14842425
            wrote on last edited by
            #5

            Thank you so much.

            1 Reply Last reply
            0
            • K kalberts

              So why not try it out? It could vary with the implementation, and with the Python version. If you try it out for yourself, you get the right answer for your installation. I would be very surprised if there was any difference at all. There will always be some random fluctuations, due to interrupts and OS activity, but that should affect both alternatives equally. In any case: In 100 iterations, the difference would be so small it would not be possible to measure. Iterate at least a few million times before drawing any conclusion.

              S Offline
              S Offline
              Superfine Construction
              wrote on last edited by
              #6

              There hardly is a difference in the work python performs for either statement:

              >>> import dis

              def inplace_add():
              ... a = 0
              ... a += 1
              ...
              def add_and_assign():
              ... a = 0
              ... a = a + 1
              ...
              dis.dis(inplace_add)
              2 0 LOAD_CONST 1 (0)
              3 STORE_FAST 0 (a)

              3 6 LOAD_FAST 0 (a)
              9 LOAD_CONST 2 (1)
              12 INPLACE_ADD
              13 STORE_FAST 0 (a)
              16 LOAD_CONST 0 (None)
              19 RETURN_VALUE

              dis.dis(add_and_assign)
              2 0 LOAD_CONST 1 (0)
              3 STORE_FAST 0 (a)

              3 6 LOAD_FAST 0 (a)
              9 LOAD_CONST 2 (1)
              12 BINARY_ADD
              13 STORE_FAST 0 (a)
              16 LOAD_CONST 0 (None)
              19 RETURN_VALUE

              1 Reply Last reply
              0
              • K kalberts

                So why not try it out? It could vary with the implementation, and with the Python version. If you try it out for yourself, you get the right answer for your installation. I would be very surprised if there was any difference at all. There will always be some random fluctuations, due to interrupts and OS activity, but that should affect both alternatives equally. In any case: In 100 iterations, the difference would be so small it would not be possible to measure. Iterate at least a few million times before drawing any conclusion.

                B Offline
                B Offline
                Brendanlala
                wrote on last edited by
                #7

                A new report from the Centers for Disease Control and Prevention confirmed that the coronavirus cases in New York City at the start of the pandemic were from Europe, prompting a new round of outrage from Governor Andrew Cuomo. "The virus came to New York, and Americans died, because of government failure," he said in call with journalists on Thursday afternoon. "These are the facts. They missed the science."

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups