CodeBeat is a line-by-line Python function tracer for debugging and performance analysis. It measures execution time per line across multiple runs, showing mean and standard deviation to identify bottlenecks in loops, branches, math operations, and list comprehensions.
- Tracks individual line execution times with statistical summaries (mean/std in ms).
- Handles nested loops, conditionals, math functions, and dynamic list operations.
- Runs functions multiple times (e.g., 10-12 iterations) for reliable profiling.
- Displays formatted tables with line numbers, code, and timing metrics.
- The source code is currently hosted on GitHub at : https://github.com/SKR18156592/CodeBeat.
- Binary installers for the latest released version are available at the Python Package Index (PyPI).
You can install codebeat using pip:
pip install codebeatImport the code_tracker module from the codebeat package, create an instance of it by passing the function you want to track as a parameter, and then utilize this instance to invoke the tracked function. When you create an instance using code_tracker(fun1), it generates a wrapper around fun1 that effectively measures and records the execution time line by line.
from codebeat import code_tracker
def fun1(x, y):
m = 0
for i in range(x):
for j in range(y):
m += i*j
return m
obj = code_tracker(fun1, 12) # Optional iteration count
result = obj(3, 4) # Executes 12x, prints timing tableSample Output:
===========================================================================================
|> Function Name: fun1, #iter:12, mean_time(in ms):0.031, std_time(in ms):0.066
===========================================================================================
| line No | Line | mean_time(in ms) | std_time(in ms)
===========================================================================================
| 0 | m = 0 | 0.000 | 0.000
| 1 | for i in range(x): | 0.012 | 0.021
| 2 | for j in range(y): | 0.008 | 0.021
| 3 | m += i*j | 0.007 | 0.020
| 4 | return m | nan | nan
-------------------------------------------------------------------------------------------
Reveals nested loop overhead—outer loop takes ~50% more time than inner.
- Matrix Sum: Compares outer vs inner loop costs.
- Branch Counter: Highlights expensive
elsebranches. - Math Series: Trig/exp functions add measurable latency.
- List Builder:
append()dominates micro-benchmarks.
CodeBeat/
├── codebeat/
│ ├── __init__.py # Exports code_tracker
│ └── code_tracker.py # Main timing logic
├── example.ipynb # Usage demos
├── LICENSE
├── README.md
├── setup.py
└── requirements.txt
MIT