Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Profile a python script using cProfile into an external file

I am new to python programming.I have a python script and I am trying to profile it using cProfile command. I typed the following

python -m cProfile -o readings.txt my_script.py

It generated readings.txt. But when I try to open the file using any standard text editor or notepad, the file doesn't open properly. It doesn't contain the data

Can anyone please tell me how to store these statistics into an external file that can be opened using notepad??

I am using windows platform

like image 710
user3217603 Avatar asked Jan 31 '14 00:01

user3217603


People also ask

How do I use python profiling?

Method 3: Python cProfile Python includes a built-in module called cProfile which is used to measure the execution time of a program. The cProfiler module provides all information about how long the program is executing and how many times the function gets called in a program.


1 Answers

The output file generated by the cProfile -o module isn't plaintext; it's a serialized pstats.Stats object. Rather than using the -o option, I generally just redirect stdout into the file I want to create.

python -m cProfile -s time my_script.py > profile.text 2>&1

Otherwise, you just need to use the pstats module to read the file and inspect its contents (see the documentation linked above).

like image 121
Henry Keiter Avatar answered Sep 27 '22 18:09

Henry Keiter