The place to be evaluated may be different between map and list comprehension notation

When using python2.7.10

When I tried using such a script, The result is that the processing speed of map is faster if it is simple. (Memory usage is delicate)

import sys

@profile
def which_is_good():
    print sys.version

    l = range(100000)

    mapped = map(lambda d: chr(65 + d%127), l)
    m_stringed = str(mapped[:])
    del mapped
    del m_stringed

    comped = [chr(65 + d%127) for d in l]
    c_stringed = str(comped[:])
    del comped
    del c_stringed

    mapped, comped = (), ()

which_is_good()

kernprof -vl run.py

2.7.10 (default, Jul 13 2015, 12:05:58) 
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)]
Wrote profile results to run.py.lprof
Timer unit: 1e-06 s

Total time: 0.155445 s
File: run.py
Function: which_is_good at line 3

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
     3                                           @profile
     4                                           def which_is_good():
     5         1           33     33.0      0.0      print sys.version
     6                                           
     7         1         2086   2086.0      1.3      l = range(100000)
     8                                           
     9         1        49070  49070.0     31.6      mapped = map(lambda d: chr(65 + d%127), l)
    10         1        21482  21482.0     13.8      m_stringed = str(mapped[:])
    11         1          150    150.0      0.1      del mapped
    12         1           23     23.0      0.0      del m_stringed
    13                                           
    14    100001        68510      0.7     44.1      comped = [chr(65 + d%127) for d in l]
    15         1        13911  13911.0      8.9      c_stringed = str(comped[:])
    16         1          152    152.0      0.1      del comped
    17         1           27     27.0      0.0      del c_stringed
    18                                           
    19         1            1      1.0      0.0      mapped, comped = (), ()

python -m memory_profiler run.py

2.7.10 (default, Jul 13 2015, 12:05:58) 
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)]
Filename: run.py

Line #    Mem usage    Increment   Line Contents
================================================
     3   19.551 MiB    0.000 MiB   @profile
     4                             def which_is_good():
     5   19.555 MiB    0.004 MiB       print sys.version
     6                             
     7   22.660 MiB    3.105 MiB       l = range(100000)
     8                             
     9   23.426 MiB    0.766 MiB       mapped = map(lambda d: chr(65 + d%127), l)
    10   28.496 MiB    5.070 MiB       m_stringed = str(mapped[:])
    11   28.246 MiB   -0.250 MiB       del mapped
    12   27.996 MiB   -0.250 MiB       del m_stringed
    13                             
    14   28.730 MiB    0.734 MiB       comped = [chr(65 + d%127) for d in l]
    15   28.840 MiB    0.109 MiB       c_stringed = str(comped[:])
    16   28.590 MiB   -0.250 MiB       del comped
    17   28.340 MiB   -0.250 MiB       del c_stringed
    18                             
    19   28.340 MiB    0.000 MiB       mapped, comped = (), ()

Recommended Posts

The place to be evaluated may be different between map and list comprehension notation
[Introduction to Python] What is the difference between a list and a tuple?
[Introduction to Udemy Python3 + Application] 60. List comprehension notation
The and operator of Python's evaluation expression seems to be evaluated from the left side expression
The timing when the value of the default argument is evaluated is different between Ruby and Python.
[Python] I tried to summarize the array, dictionary generation method, loop method, list comprehension notation
Extract and list personal names and place names in the text
How to use argparse and the difference between optparse
[Python] It might be useful to list the data frames
Understand the difference between cumulative assignment to variables and cumulative assignment to objects
[Python] Comprehension notation. Write a for statement simply. (A collection is the difference between an iterator and an iterator)