Experience Python's comprehension time
It seems that the comprehension is fast, but the description method is difficult. You need to get used to it. If you get used to it, you may be able to use it for code tests.
Perform the process of adding numbers to the list
Measure the time with time python test.py
.
test.py
lists = []
for i in range(100000000):
lists.append(i**2)
test.py
lists = [i**2 for i in range(100000000)]
Code 1
real 0m36.245s
user 0m34.322s
sys 0m1.848s
Code 2
real 0m30.278s
user 0m26.673s
sys 0m3.225s
The inclusion notation is faster! Perhaps it will be faster due to conditional branching or more continuous for loops. In the code test production, I thought that I should write in a method that I know, such as time limit. I would like to be able to use these technologies for code testing where small capacity and high-speed execution are also issues!
(Judgment is done only once, and I only take code tests that make it difficult to reproduce standard input, so it's hard to think about it later and measure how fast it is.)
Recommended Posts