Compare the speed of Python append and map

I will reprint what I wrote for the company

Purpose

Check what the difference between the processing speed and memory usage of the code you wrote first (using append) and the code pointed out in the review (using map) is

Verification code

Prepare a program that merges the two arrays and outputs them. (Originally it was a program that merges fields and rows fetched from DB and outputs it as json)

The tools used for measurement are as follows.

--memory_profiler: You can measure the increase or decrease of memory in units of lines. You can also check the current usage and increment --line_profiler: Processing speed can be measured line by line.

Verification 1


from memory_profiler import profile
from line_profiler import LineProfiler


@profile(precision=8)
def main():
    a = [1, 2, 3]
    b = ['1', '2', '3']

    results = []
    for _ in range(1000):
        results.append(dict(zip(a, b)))

    for r in results:
        print(r) # {1: '1', 2: '2', 3: '3'}


if __name__ == "__main__":
    #When measuring memory
    #    main()
    
    #When comparing processing speeds
    prof = LineProfiler()
    prof.add_function(main)
    prof.runcall(main)
    prof.print_stats(output_unit=1e-9)

Verification 2

Except for the contents of the method, it is the same as validation 1, so it will be omitted.

def main():
    a = [1, 2, 3]
    b = ['1', '2', '3']

    results = map(dict, (zip(a, b) for _ in range(1000)))

    for r in results:
        print(r) # {1: '1', 2: '2', 3: '3'}

result

The amount of data was small in the first place, but the difference was noticeable.

processing speed

Verification 1

Total time: 0.087002 s
File: append.py
Function: main at line 6

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
     6                                           def main():
     7         1       4000.0   4000.0      0.0      a = [1, 2, 3]
     8         1       1000.0   1000.0      0.0      b = ['1', '2', '3']
     9                                           
    10         1          0.0      0.0      0.0      results = []
    11      1001     395000.0    394.6      0.5      for _ in range(1000):
    12      1000    1973000.0   1973.0      2.3          results.append(dict(zip(a, b)))
    13                                           
    14      1001    5854000.0   5848.2      6.7      for r in results:
    15      1000   78775000.0  78775.0     90.5          print(r)

Verification 2

Total time: 0.069483 s
File: map.py
Function: main at line 7

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
     7                                           def main():
     8         1       4000.0   4000.0      0.0      a = [1, 2, 3]
     9         1       1000.0   1000.0      0.0      b = ['1', '2', '3']
    10                                           
    11         1       2000.0   2000.0      0.0      results = map(dict, (zip(a, b) for _ in range(1000)))
    12                                           
    13      1001    8476000.0   8467.5     12.2      for r in results:
    14      1000   61000000.0  61000.0     87.8          print(r)

memory usage

Verification 1

Line #    Mem usage    Increment   Line Contents
================================================
     5  38.28125000 MiB  38.28125000 MiB   @profile(precision=8)
     6                             def main():
     7  38.28515625 MiB   0.00390625 MiB       a = [1, 2, 3]
     8  38.28515625 MiB   0.00000000 MiB       b = ['1', '2', '3']
     9                             
    10  38.28515625 MiB   0.00000000 MiB       results = []
    11  38.38671875 MiB   0.00390625 MiB       for _ in range(1000):
    12  38.38671875 MiB   0.00390625 MiB           results.append(dict(zip(a, b)))
    13                             
    14  38.39453125 MiB   0.00000000 MiB       for r in results:
    15  38.39453125 MiB   0.00781250 MiB           print(r)

Verification 2

Line #    Mem usage    Increment   Line Contents
================================================
     5  38.22656250 MiB  38.22656250 MiB   @profile(precision=8)
     6                             def main():
     7  38.23046875 MiB   0.00390625 MiB       a = [1, 2, 3]
     8  38.23046875 MiB   0.00000000 MiB       b = ['1', '2', '3']
     9                             
    10  38.23828125 MiB   0.00000000 MiB       results = map(dict, (zip(a, b) for _ in range(1000)))
    11                             
    12  38.23828125 MiB   0.00000000 MiB       for r in results:
    13  38.23828125 MiB   0.00781250 MiB           print(r)

in conclusion

Please let me know if there is something strange.

Recommended Posts

Compare the speed of Python append and map
The story of Python and the story of NaN
I tried to compare the processing speed with dplyr of R and pandas of Python
I replaced the numerical calculation of Python with Rust and compared the speed
Summary of the differences between PHP and Python
The answer of "1/2" is different between python2 and 3
Specifying the range of ruby and python arrays
Ruby, Python and map
the zen of Python
Open an Excel file in Python and color the map of Japan
About the * (asterisk) argument of python (and itertools.starmap)
A discussion of the strengths and weaknesses of Python
I measured the speed of list comprehension, for and while with python2.7.
I compared the speed of go language web framework echo and python web framework flask
I compared the speed of regular expressions in Ruby, Python, and Perl (2013 version)
The story of Python without increment and decrement operators.
The process of installing Atom and getting Python running
Python netCDF4 read speed and nesting of for statements
Python --Explanation and usage summary of the top 24 packages
Visualize the range of interpolation and extrapolation with python
I checked out the versions of Blender and Python
[Python] Correct usage of map
python3 Measure the processing speed.
Towards the retirement of Python2
Compare the Python array-like guys
Compare the fonts of jupyter-themes
About the ease of Python
About the features of Python
Source installation and installation of Python
The Power of Pandas: Python
Compare read / write speed and capacity of csv, pickle, joblib, parquet in python environment
The result of making a map album of Italy honeymoon in Python and sharing it
I compared the speed of the reference of the python in list and the reference of the dictionary comprehension made from the in list.
[Python] Heron's formula functionalization and calculation of the maximum area
[Maya Python] Crush the contents of the script 1 ~ Camera Speed Editor
The story of Python and the story of NaN
Python nan check
[python] plot the values ​​before and after the conversion of yeojohnson conversion
The process of making Python code object-oriented and improving it
The websocket of toio (nodejs) and python / websocket do not connect.
I want to know the features of Python and pip
[Tips] Problems and solutions in the development of python + kivy
Play with the password mechanism of GitHub Webhook and Python
Installation of SciPy and matplotlib (Python)
[Python] The stumbling block of import
First Python 3 ~ The beginning of repetition ~
Existence from the viewpoint of Python
pyenv-change the python version of virtualenv
[Python] Adjusted the color map standard
Change the Python version of Homebrew
This and that of python properties
[Python] Understanding the potential_field_planning of Python Robotics
Review of the basics of Python (FizzBuzz)
Compare Python and JavaScript array loops
Speed comparison of Python XML parsing
python string processing map and lambda
Coexistence of Python2 and 3 with CircleCI (1.0)
About the basics list of Python basics
Summary of Python indexes and slices
Reputation of Python books and reference books
Learn the basics of Python ① Beginners
Count the number of Thai and Arabic characters well in Python