I like Python's comprehension, so I compared it with map

Introduction

Hello, this is mana binding. There is no doubt that it is a versatile, readable and wonderful grammar among the notations of programming languages with many inclusions in Python (it is an individual opinion and may be very prejudiced), but in competitive programming it is often: You can see the notation of.

l = list(map(int, input().split()))

It's the enemy of the inclusion notation, map (prejudice (omitted). The map that must be bitten by list is a monjayaki! So I compared the speed.

environment

I use Ubuntu of wsl1.

$ uname -a
Linux LAPTOP-H6KC5C7N 4.4.0-18362-Microsoft #1049-Microsoft Thu Aug 14 12:01:00 PST 2020 x86_64 x86_64 x86_64 GNU/Linux
$ py --version
Python 3.8.0

Overview

1 10000 Prepare a text file with non-negative integer values in rows and 5 columns. Write 10000 at the beginning to make the number of columns in the file. Use time () in the 2 time module to record the start time. Store data in comprehension or map for each 3 line. After repeating 4 data.append, get the time with time (), get the difference from the value of 2, and display it as the execution time. 5 Try about 5 times and compare the averages of them.

script

Use the following script.

Comprehension notation


import time

start = time.time()
n = int(input())
data = []
for _ in range(n):
    data.append([int(e) for e in input().split(" ")])
process_time = time.time() - start

print(process_time)

map


import time

start = time.time()
n = int(input())
data = []
for _ in range(n):
    data.append(list(map(int, input().split())))
process_time = time.time() - start

print(process_time)

Verification

Try running it about five times and think about the average. First is the inclusion notation. Well, it's fair.

Comprehension notation


$ for((i=0;i<5;i++)); do cat InputText.txt | py time_comp.py; done
0.0833895206451416
0.07289266586303711
0.08693099021911621
0.12533855438232422
0.09039974212646484

Is it about 0.09541 on average? Next is map.

map


$ for((i=0;i<5;i++)); do cat InputText.txt | py time_map.py; done
0.0980367660522461
0.08674144744873047
0.11994338035583496
0.08462047576904297
0.08770060539245605

This is also the same as 0.09541 on average. (No, there is no significant difference ... I thought I'd write an article because the inclusion notation is slow!)

Additional verification

I did it with Raspberry Pi 4.

$ uname -a
Linux asana 5.4.51-v8+ #1333 SMP PREEMPT Mon Aug 10 16:58:35 BST 2020 aarch64GNU/Linux

$ py --version
Python 3.7.3

$ for((i=0;i<5;i++)); do cat InputText.txt | py time_comp.py; done
0.12080025672912598
0.10874629020690918
0.1127462387084961
0.1103978157043457
0.15588116645812988

$ for((i=0;i<5;i++)); do cat InputText.txt | py time_map.py; done
0.11949372291564941
0.11281895637512207
0.11392450332641602
0.2708289623260498
0.276080846786499

If you calculate the average, the inclusion notation is about 0.1217 and the map is about 0.1786. Inclusive notation has become dominant here.

I did it on Windows.

> cmd /c ver

Microsoft Windows [Version 10.0.18363.1110]
> py --version
Python 3.8.2

> for($i = 0;$i -le 5; $i++){cat .\InputText.txt | py .\time_comp.py}
1.0488629341125488
0.7845804691314697
1.163966178894043
0.7295846939086914
0.7399096488952637
0.8466687202453613

> for($i = 0;$i -le 5; $i++){cat .\InputText.txt | py .\time_map.py}
0.5758388042449951
0.5823671817779541
0.6683478355407715
0.6919825077056885
0.6597652435302734
0.6140100955963135

~~ Windows Oops ~~ There was a clear significant difference, but it seems that there are quite a few differences depending on the OS.

Conclusion

In conclusion, "It is easy to be influenced by the environment and the situation, and I cannot say which is better. Is it like that? I thought that map was used more often because it was obviously faster, but that doesn't seem to be the case. Or will there be a difference in other architectures and OSs ... Will it change if input is set to sys.stdin, restore pending is slow in the first place and the difference is hidden, how is memory efficiency, etc. There seems to be some verification items, but I would like to end it here. Thank you for reading this far. Which party are you?

Recommended Posts

I like Python's comprehension, so I compared it with map
W3C Validators didn't work with Sublime Text3 so I made it work
I got stuck so I reviewed it
I made a segment tree with python, so I will introduce it
I got an SSL related error with pip install, so I solved it
I tried to make a calculator with Tkinter so I will write it
I researched Docker, so I will summarize it
I made a Hex map with Python
I compared Python's iterator and Ruby's Enumerator
I studied about Linux, so I summarized it.
#I tried something like Vlookup with Python # 2
I was able to mock AWS-Batch with python, moto, so I will leave it
I customized it with Visual Studio Code (mainly for python), so I will summarize it
[Python] I introduced Word2Vec and played with it.
Try drawing a map with Python's folium package
Code review with machine learning Amazon Code Guru now supports Python so I tried it
I got an error when pip install pandas on Mac, so I dealt with it
I wanted to operate google spread sheet with AWS lambda, so I tried it [Part 2]
I don't like to be frustrated with the release of Pokemon Go, so I made a script to detect the release and tweet it