Which one would be faster in Python i = i + 1 or i += 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?
-
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?
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.
-
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.
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?
-
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?
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.38091952400282Obviously, the "# ..." indicates where you put you one-million iterations loop.
-
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.38091952400282Obviously, the "# ..." indicates where you put you one-million iterations loop.
Thank you so much.
-
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.
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_VALUEdis.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 -
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.
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."