[PYTHON] Techniques for code testing?

Purpose

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.

experimental method

Perform the process of adding numbers to the list Measure the time with time python test.py.

Code 1 Normal

test.py


lists = []
for i in range(100000000):
    lists.append(i**2)
Code 2 comprehension

test.py


lists = [i**2 for i in range(100000000)]

Conclusion

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

Techniques for code testing?
Python code memo for yourself
Test code for evaluating decorators
Techniques for sorting in Python
[Python] Sample code for Python grammar
Decision tree (for beginners) -Code edition-
Python course for data science_useful techniques
VS Code snippets for data analysts
Logistic Regression (for beginners) -Code Edition-
A note for writing Python-like code
Linear regression (for beginners) -Code edition-
Code for self-organizing state-space model (unfinished)
Ridge Regression (for beginners) -Code Edition-
Web server for browser testing with Mocha
(For myself) Put Flask in VS Code
Detailed Python techniques required for data shaping (1)
python> coding guide> PEP 0008 --Style Guide for Python Code
Support Vector Machine (for beginners) -Code Edition-
Summary of petit techniques for Linux commands
I wrote the code for Gibbs sampling
A tool for easily entering Python code
Code required for API conversion in sqlalchemy
Summary of useful techniques for Python Scrapy
Code for self-analysis and radar chart creation
Detailed Python techniques required for data shaping (2)
Code for self-organizing state-space model (modified version)
R code compatible sheet for Python users