I tried to find out the difference between A + = B and A = A + B in Python, so make a note

Experiment environment

OS macOS Sierra
CPU 2.3 GHz Intel Core i7
memory 16 GB 1600 MHz DDR3
Python version 2.7.10

Experiment

Preparation

def add(list_x, list_y):                                                        
    list_x = list_x + list_y                                                    
    return list_x   

def inplace(list_x, list_y):                                                    
    list_x += list_y                                                            
    return list_x 

list_a = [[0 for x in range(10000)] for y in range(10000)]
list_b = [[0 for x in range(10000)] for y in range(10000)]

Experimental result

add(list_a, list_b)
Execution time: 0.000458
id(list_a): 4391953400 -> 5530614960
------ dis.dis(add) ------
  8           0 LOAD_FAST                0 (list_a)
              3 LOAD_FAST                1 (list_b)
              6 BINARY_ADD          
              7 STORE_FAST               0 (list_a)

  9          10 LOAD_FAST                0 (list_a)
             13 RETURN_VALUE
   
  
inplace(list_a, list_b)
Execution time: 0.000285
id(list_a): 5530614960 -> 5530614960
------ dis.dis(inplace)  ------
 44           0 LOAD_FAST                0 (list_a)
              3 LOAD_FAST                1 (list_b)
              6 INPLACE_ADD         
              7 STORE_FAST               0 (list_a)

 45          10 LOAD_FAST                0 (list_a)
             13 RETURN_VALUE        

add inplace result
Execution time 0.000458 0.000285 in place is faster
Bytecode difference BINARY_ADD INPLACE_ADD You can see that it has changed even with bytecode
id(list_a) There is a change No change add is created as a new object and replace is assigned a value to the same object.

motivation

I had never been aware of the internal movements of both parties, so I investigated it myself. As long as I'm embarrassed when I was thinking, "Let's do it together!"

Recommended Posts

I tried to find out the difference between A + = B and A = A + B in Python, so make a note
[Python] I tried to implement stable sorting, so make a note
I tried to find out the difference between A + = B and A = A + B in Python, so make a note
Let's make a simple game with Python 3 and iPhone
Build a Python environment and transfer data to the server
I tried to enumerate the differences between java and python
I tried to make a stopwatch using tkinter in python
I also tried to imitate the function monad and State monad with a generator in Python
[Introduction to Python] What is the difference between a list and a tuple?
About the difference between "==" and "is" in python
A note I looked up to make a command line tool in Python
I don't really understand the difference between modules, packages and libraries, so I tried to organize them.
I tried to make a periodical process with CentOS7, Selenium, Python and Chrome
I set up TensowFlow and was addicted to it, so make a note
I tried to graph the packages installed in Python
I tried to implement a pseudo pachislot in Python
Find the difference in Python
I wanted to use the find module of Ansible2, but it took some time, so make a note
I tried to find out how to streamline the work flow with Excel x Python ②
I tried to find out how to streamline the work flow with Excel x Python ④
I tried to find out how to streamline the work flow with Excel x Python ⑤
I just want to find the 95% confidence interval for the difference in population ratios in Python
I tried to find out how to streamline the work flow with Excel x Python ①
I used Python to find out about the role choices of the 51 "Yachts" in the world.
A Python beginner made a chat bot, so I tried to summarize how to make it.
I tried to find out how to streamline the work flow with Excel x Python ③
I tried to find the entropy of the image with python
I tried to find out the outline about Big Gorilla
Find out the apparent width of a string in python
I tried to implement a one-dimensional cellular automaton in Python
I tried "How to get a method decorated in Python"
I tried programming the chi-square test in Python and Java.
I created a class in Python and tried duck typing
I tried to implement the mail sending function in Python
I tried to make GUI tic-tac-toe with Python and Tkinter
I want to make input () a nice complement in python
(Python: OpenCV) I tried to output a value indicating the distance between regions while binarizing the video in real time.
Difference between list () and [] in Python
Difference between == and is in python
python beginners tried to find out
I want to absorb the difference between the for statement on the Python + numpy matrix and the Julia for statement
I tried to create a Python script to get the value of a cell in Microsoft Excel
I wrote a doctest in "I tried to simulate the probability of a bingo game with Python"
I tried to make a function to judge whether the major stock exchanges in the world are daylight saving time with python
[5th] I tried to make a certain authenticator-like tool with python
[2nd] I tried to make a certain authenticator-like tool with python
I tried to make a regular expression of "amount" using Python
I tried to make a regular expression of "time" using Python
[3rd] I tried to make a certain authenticator-like tool with python
I tried to make a regular expression of "date" using Python
I tried to implement a misunderstood prisoner's dilemma game in Python
I tried to find out if ReDoS is possible with Python
I tried to make a 2channel post notification application with Python
I tried to make a todo application using bottle with python
[4th] I tried to make a certain authenticator-like tool with python
I tried to cut out a still image from the video
[1st] I tried to make a certain authenticator-like tool with python
When I tried to scrape using requests in python, I was addicted to SSLError, so a workaround memo
[Python] I tried to make a simple program that works on the command line using argparse.
A story that didn't work when I tried to log in with the Python requests module
I tried "Implementing a genetic algorithm (GA) in python to solve the traveling salesman problem (TSP)"
I tried to implement PLSA in Python
I tried to implement permutation in Python