Check the processing time and the number of calls for each process in python (cProfile)

Introduction

When analyzing data, my own code is often slow. There is also a method of measuring the time by using the time function on an ad hoc basis, but this time I would like to use Profiling.

Easy to use

  1. Grant the cProfile command at runtime
python -m cProfile -o File name to write profile to.prof File name for which you want to measure the processing time.py
  1. Create code to look inside the prof file
import pstats


if __name__ == "__main__":
    sts = pstats.Stats("hoge.prof")
    sts.strip_dirs().sort_stats("cumtime").print_stats(30)
  1. Execute the above py file
Mon Jun 15 02:22:15 2020    hoge.prof

         415990 function calls (415842 primitive calls) in 0.742 seconds

   Ordered by: cumulative time
   List reduced from 350 to 30 due to restriction <30>

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
      7/1    0.000    0.000    0.742    0.742 {built-in method builtins.exec}
        1    0.000    0.000    0.742    0.742 hoge.py:1(<module>)
        1    0.052    0.052    0.734    0.734 hoge.py:54(main)
    13338    0.337    0.000    0.337    0.000 {built-in method builtins.print}
    13337    0.013    0.000    0.284    0.000 {built-in method strptime}
    13337    0.022    0.000    0.269    0.000 _strptime.py:574(_strptime_datetime)
    13337    0.136    0.000    0.248    0.000 _strptime.py:318(_strptime)
    13339    0.008    0.000    0.056    0.000 _strptime.py:26(_getlang)
    13339    0.010    0.000    0.048    0.000 locale.py:571(getlocale)
    13339    0.007    0.000    0.027    0.000 locale.py:467(_parse_localename)
    11983    0.019    0.000    0.023    0.000 hoge.py:41(nxt_hash)
    13339    0.014    0.000    0.020    0.000 locale.py:384(normalize)
    13337    0.020    0.000    0.020    0.000 {method 'match' of 're.Pattern' objects}
    13338    0.011    0.000    0.016    0.000 hoge.py:11(read_files)
    13337    0.013    0.000    0.013    0.000 {method 'groupdict' of 're.Match' objects}
    13339    0.011    0.000    0.011    0.000 {built-in method _locale.setlocale}
     11/4    0.000    0.000    0.010    0.002 <frozen importlib._bootstrap>:978(_find_and_load)
     11/4    0.000    0.000    0.010    0.002 <frozen importlib._bootstrap>:948(_find_and_load_unlocked)
     11/4    0.000    0.000    0.009    0.002 <frozen importlib._bootstrap>:663(_load_unlocked)
      6/4    0.000    0.000    0.009    0.002 <frozen importlib._bootstrap_external>:722(exec_module)
    13337    0.009    0.000    0.009    0.000 {method 'split' of 'str' objects}
    13337    0.008    0.000    0.008    0.000 {method 'format' of 'str' objects}
    40114    0.008    0.000    0.008    0.000 {method 'get' of 'dict' objects}
     16/4    0.000    0.000    0.008    0.002 <frozen importlib._bootstrap>:211(_call_with_frames_removed)
    26674    0.004    0.000    0.004    0.000 {method 'keys' of 'dict' objects}
    26675    0.004    0.000    0.004    0.000 {method 'toordinal' of 'datetime.date' objects}
    27406    0.004    0.000    0.004    0.000 {built-in method builtins.isinstance}
27111/27083    0.004    0.000    0.004    0.000 {built-in method builtins.len}
     2947    0.003    0.000    0.003    0.000 {method 'hexdigest' of '_hashlib.HASH' objects}
    13682    0.003    0.000    0.003    0.000 {method 'rstrip' of 'str' objects}

argument of sort_stats

Reference

The arguments of print_stats are listed up to the top K.

・ Ncalls: Number of calls -Tottime: Total time spent on the function of interest (excluding that time if another call is made internally) ・ Cumtime: Calculation time including internal call of tottime

Recommended Posts

Check the processing time and the number of calls for each process in python (cProfile)
Check the operation of Python for .NET in each environment
[Python] Measures and displays the time required for processing
plot the coordinates of the processing (python) list and specify the number of times in draw ()
Get the number of occurrences for each element in the list
A function that measures the processing time of a method in python
Check the behavior of destructor in Python
MongoDB for the first time in Python
Status of each Python processing system in 2020
Put the process to sleep for a certain period of time (seconds) or more in Python
The story of returning to the front line for the first time in 5 years and refactoring Python Django
python> array> Determine the number and initialize> mylist = [idx for idx in range (10)] / mylist = [0 for idx in range (10)] >> mylist = [0] * 10
Output the number of CPU cores in Python
View the result of geometry processing in Python
Image processing? The story of starting Python for
[IOS] Change the display time for each frame of GIF animation in Pythonista3.
Divides the character string by the specified number of characters. In Ruby and Python.
Check if the string is a number in python
Full-width and half-width processing of CSV data in Python
How to get the number of digits in Python
The process of installing Atom and getting Python running
Get the size (number of elements) of UnionFind in Python
Check for the existence of BigQuery tables in Java
Check the asymptotic nature of the probability distribution in Python
Summary of date processing in Python (datetime and dateutil)
Output the specified table of Oracle database in Python to Excel for each file
How to count the number of occurrences of each element in the list in Python with weight
Get the number of specific elements in a python list
Python --Find out number of groups in the regex expression
[Understand in the shortest time] Python basics for data analysis
[Homology] Count the number of holes in data with Python
[Python] Display the elapsed time in hours, minutes, and seconds (00:00:00)
Get the current date and time in Python, considering the time difference
The process of making Python code object-oriented and improving it
[Tips] Problems and solutions in the development of python + kivy
Coordination of each process in MPI and buffering of standard output
Google search for the last line of the file in Python
Get the index of each element of the confusion matrix in Python
The story of creating a "spirit and time chat room" exclusively for engineers in the company
Tips for Python beginners to use the Scikit-image example for themselves 8 Processing time measurement and profiler
Check for memory leaks in Python
Determine the date and time format in Python and convert to Unixtime
The story of Python and the story of NaN
Check for external commands in python
I compared the calculation time of the moving average written in Python
Wrap (part of) the AtCoder Library in Cython for use in Python
Check the increase / decrease of Bitcoin for each address from the blockchain
[Python] Let's reduce the number of elements in the result in set operations
How to check the memory size of a variable in Python
Automatically resize screenshots for the App Store for each screen in Python
How to check the memory size of a dictionary in Python
Check and move directories in Python
The story of releasing a Python text check tool on GitHub x CircleCI for the first time
Process the result of% time,% timeit
Dockerfile with the necessary libraries for natural language processing in python
See python for the first time
python note: map -do the same for each element of the list
for, continue, break Explain the flow of iterative processing in Python3-Part 1
Get the title and delivery date of Yahoo! News in Python
Try using FireBase Cloud Firestore in Python for the time being
Get the number of readers of a treatise on Mendeley in Python