Skip to content

SKR18156592/CodeBeat

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CodeBeat

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.

Features

  • 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.

Where to get it

Installation

You can install codebeat using pip:

pip install codebeat

Quick Start

Import 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 table

Sample 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.

Examples

  • Matrix Sum: Compares outer vs inner loop costs.
  • Branch Counter: Highlights expensive else branches.
  • Math Series: Trig/exp functions add measurable latency.
  • List Builder: append() dominates micro-benchmarks.

Project Structure

CodeBeat/
├── codebeat/
│   ├── __init__.py      # Exports code_tracker
│   └── code_tracker.py  # Main timing logic
├── example.ipynb        # Usage demos
├── LICENSE
├── README.md
├── setup.py
└── requirements.txt

License

MIT

About

A line-by-line Python function tracer for debugging and performance analysis

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors