2

The execution times for i += 1 and i = i + 1 are different. For example, given the following snippet:

snip = """
x = 0
for i in range(10000):
    x += i
"""

snip2 = """x=0
for i in range(10000):
    x = x + i
"""

First run it once:

print timeit.timeit(snip, number=1)
print timeit.timeit(snip2, number=1)
0.000745058059692
0.000648021697998

and x = x + i is faster than x += i, run it a thousand times:

print timeit.timeit(snip, number=1000)
print timeit.timeit(snip2, number=1000)

0.663557052612
0.661299943924

then both result in nearly the same time, off by few microseconds, and run it a million times:

print timeit.timeit(snip, number=1000000)
print timeit.timeit(snip2, number=1000000)
771.912870884
778.865171909

then it clearly results in the opposite of case one; x += i is cheaper now and the other is way costlier.

What is the difference in execution of those two statements? What is Big O time for both?

This question originates from a situation where my server has statements i += 1 and billions of DB transactions will end in dead lock, but with i = i + 1 it works great.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
ifixthat
  • 6,137
  • 5
  • 25
  • 42
  • 1
    Note that *"Big O time"* is exactly what you're **not** looking at here - simple arithmetic operations are `O(1)`! – jonrsharpe Nov 06 '15 at 17:42
  • 2
    Are your results replicable? is `x+= 1` faster by seven seconds 10 times out of ten? – Jeremy Kemball Nov 06 '15 at 17:43
  • 1
    Whether your code is open to deadlocks is around semantic differences, not performance ones. `__add__` and `__iadd__` aren't the same thing. – Charles Duffy Nov 06 '15 at 17:47
  • Micro-benchmarks are notoriously unreliable. How many times have you run this? Did you restart the interpretter between invokations? Did you have a warm up period? – Elliott Frisch Nov 06 '15 at 17:49
  • These are two different lines are code where any difference in performance would be miniscule and not worth worrying about. Go for what you find more readable and manageable – Sayse Nov 06 '15 at 17:50
  • 1
    Well, I'm pretty sure changing `i += 1` to `i = i + 1` is **not** a correct fix for your dead lock. I mean: there is an other issue and you are just happening to avoid the problem. But at the next code change it could come up again. – Bakuriu Nov 06 '15 at 17:50
  • @ElliottFrisch, ...if this is CPython, there's no JIT, so I'm not entirely sure that a warm-up period and a fresh start are relevant. (For Jython or PyPi it'd be a whole different story). – Charles Duffy Nov 06 '15 at 17:52
  • @Bakuriu: May be that was quick fix and that leaded me to this question. thanks for pointing though – ifixthat Nov 06 '15 at 17:54
  • @Andrew: yeah I tried searching ir but didn't found that some how, but that perfect thanks, I have also flaged the question too duplicate and close it. – ifixthat Nov 06 '15 at 17:55
  • @Sayse why not to worry about it, if it can make considerable difference on your execution time, I can try to find the root of it and see what can be better for code. thanks though – ifixthat Nov 06 '15 at 17:56
  • @shodowsjedi there will always be other parts of your code where you can gain greater performance increases – Sayse Nov 06 '15 at 18:35

0 Answers0