5

I'm trying to run cProfile on my python script and all I care about is the total time it took to run. Is there a way to modify

python -m cProfile myscript.py

so the output is just the total time?

Adam Kall
  • 85
  • 1
  • 6

1 Answers1

1

This answers supposes that you are using a Unix terminal. The fastest thing I can think of would be to redirect the results into a file with the ">" operator and then read the file with head, something like:

python -m cProfile your_python_file.py > temp_file && head -n 3 temp_file 

So basically, to explain my self further, you are writing all the results of the profiling into temp_file (this file will get created if it doesn't exist and its name does not really matter ). And after this you display the first 3 lines of this file with head -n 3 . Of course you will have to manually delete temp_file if you do not need it! Hope this helps!

mkarts
  • 667
  • 5
  • 10
  • Can I not somehow add the -n 3 to the initial command itself? Sorry if that's a stupid question I'm just trying to speed up my job of recording times at various settings and opening a temp file wouldn't help – Adam Kall Jun 27 '16 at 16:52
  • you could try to do head -n 3 | python -m cProfile your_file.py , but I think you ll get an IOError, or at least that's what I get – mkarts Jun 27 '16 at 16:55
  • well you can also add && rm temp_file to automatically clean the file :D and do all you want in one line. – mkarts Jun 27 '16 at 16:56