neuralib.util.profile

profile_test

trace_line

Decorator for trace line number in real time using threading.

Profile

This module provides utilities for profiling and tracing execution of code.

It includes:

  • profile_test: A context manager for profiling code execution using the built-in cProfile module. The profiler can dump the stats in various formats: - .dat: Raw profile dump. - .txt: Human-readable text output. - .png: Graphical representation using gprof2dot and Graphviz’s dot tool.

  • trace_line: A decorator that launches a background thread to trace and print the current line number being executed at regular intervals. This is especially useful when debugging long-running processes that might be terminated by the operating system without a traceback.

Usage Examples

Profiling Example:

with profile_test(enable=True, output_file='profile.txt'):
    # Code to profile
    do_something()

Trace Line Example:

@trace_line(interval=0.1)
def long_running_function():
    # Some long-running process
    process_data()

long_running_function()