[Small story] In Python, i = i + 1 is slightly faster than i + = 1.

As the title says. It's not a heavy process to begin with, so I don't think it's going to happen to a slow program when I know this, but it was surprising, so as a small story.

code


from __future__ import print_function
import timeit

print("i+=1\t", timeit.timeit("while i<1000000: i+=1", setup="i=0"))
print("i=i+1\t", timeit.timeit("while i<1000000: i=i+1", setup="i=0"))

print("a[0]+=1\t", timeit.timeit("while a[0]<1000000: a[0]+=1", setup="a=[0]"))
print("a[0]=a[0]+1\t", timeit.timeit("while a[0]<1000000: a[0]=a[0]+1", setup="a=[0]"))

text:Python2.7.8 Execution result


i+=1     0.0610518455505
i=i+1    0.0599188804626
a[0]+=1  0.108623981476
a[0]=a[0]+1      0.108937978745

text:Python3.4.1 Execution result


i+=1     0.08549419100017985
i=i+1    0.07950809699832462
a[0]+=1  0.14678418899711687
a[0]=a[0]+1      0.1382706459990004

Python3 is slower. I did not know. Anyway. It turns out that ʻi = i + 1 is faster than ʻi + = 1, albeit very slightly.

I haven't investigated it in detail, but the cause is probably this. When you add assignments in the form ʻi + = 1, the Python interpreter first tries to find ʻi .__ iadd__. And if it doesn't exist, look for ʻi .__ add__. For additions of the form ʻi = i + 1, the Python interpreter looks for ʻi .__ add__ from the beginning. So, neither Python2 nor Python3 has ʻint .__ iadd__. Therefore, the processing of ʻi + = 1 is wasteful because it fails by searching for ʻint.__ iadd__. Therefore, the ʻi = i + 1` format is probably faster.

However, I thought that ʻa [0] + = 1 would be faster than ʻa [0] = a [0] + 1, which requires two array references. If you think about it carefully, it seems faster to refer to the 0th of the array more than to search for a method.

Recommended Posts

[Small story] In Python, i = i + 1 is slightly faster than i + = 1.
@ Is faster than dot
python small story collection
I wrote python in Japanese
Python release cycle is faster!
I understand Python in Japanese!
What I learned in Python
[Small story] [Python] Replace strings in 2D arrays with numbers
Difference between == and is in python
Use fabric as is in python (fabric3)
Golang vs. Python – Is Golang Better Than Python?
Python is UnicodeEncodeError in CodeBox docker
[Python competition professional memo] LRU cache is faster than programmatic memoization
sympy.Mul is much faster than sympy.prod
I wrote Fizz Buzz in Python
[Small story] Get timestamp with Python
There is no switch in python
I learned about processes in Python
I can't install scikit-learn in Python
I wrote the queue in Python
Python in is also an operator
I tried Line notification in Python
I wrote the stack in Python
In Python 3.8, pow (n, -1, 1000000007) looks better than pow (n, 1000000007-2, 1000000007)
I put Python 2.7 in Sakura VPS 1GB.
I tried to implement PLSA in Python
I made a payroll program in Python!
Inject is recommended for DDD in Python
I tried to implement PLSA in Python 2
I tried using Bayesian Optimization in Python
I can't debug python scripts in Eclipse
What is "mahjong" in the Python library? ??
Hash in Perl is a dictionary in Python
I implemented Cousera's logistic regression in Python
I tried to implement ADALINE in Python
I wanted to solve ABC159 in Python
I tried to implement PPO in Python
ModuleNotFoundError in Python: No module named story
I searched for prime numbers in python
Tips for making small tools in python
Which is faster, Python shuffle or sample?
I created a password tool in Python.
How to use is and == in Python
Why can't I install matplotlib in python! !!
I went to "Summer is in full swing! Spark + Python + Data Science Festival".
Python program is slow! I want to speed up! In such a case ...
I want to do Dunnett's test in Python
I implemented Robinson's Bayesian Spam Filter in python
A memo that I wrote a quicksort in Python
I was able to recurse in Python: lambda
I want to create a window in Python
What is wheezy in the Docker Python image?
I tried playing a typing game in Python
When I try matplotlib in Python, it says'cairo.Context'
I tried simulating the "birthday paradox" in Python
I tried the least squares method in Python
I investigated in detail about variable processing in python
I tried Python! ] I graduated today from "What is Python! Python!"!
I wrote a class in Python3 and Java
I wrote "Introduction to Effect Verification" in Python
About the difference between "==" and "is" in python