Is there a Python library that shows execution time per line?

Question:

I have found this library called heartrate which shows how many times each line has been hit. I’m wondering if there is a similar library that can show the execution time of each line? Sometimes a single line can be a bottleneck of a program.

enter image description here

Asked By: CyberPlayerOne

||

Answers:

There is a library called pyheat.

you can install by

pip install py-heat

and import it using from pyheat import PyHeat

Pyheat can be used to generate a heatmap of time numbers for each line of code for a Python module. Pass the path of the Python file as a parameter to the PyHeat function.

ph = PyHeat('merge_sort.py')  # mergesort.py is the example given
ph.create_heatmap()
ph.show_heatmap()

Please look into this https://github.com/csurfer/pyheat

Answered By: God Is One

I’ve built a small library perf_tool that can perform this kind of benchmarks.

It can be used to profile pieces of codes and also single lines using decorators, here is a simple example:

 from perf_tool import PerfTool
 with PerfTool('test'):
    <The code to be profiled>


Calling it the result is a report showing some metrics like this:

================== PerfTool ==================
task                     |aver(s) |sum(s)  |count   |std     
main                     |   0.065|   0.065|       1|   0.000
  +-test                 |   0.050|   0.005|      10|   0.002

There are also other libraries in that field like linetimer

Answered By: Glauco