Skip to content

Feature Request: Add @functools.track_stats decorator for lightweight performance monitoring #149206

@Hans-developer

Description

@Hans-developer

Feature or enhancement

Proposal:

@functools.track_stats
def my_task():
    # process data
    pass

my_task()
print(my_task.get_stats().average_time)

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

I propose adding a new decorator called @track_stats to the functools module. This tool allows developers to monitor function performance (call count and execution time) in a simple and "pythonic" way without the overhead of full profiling tools like cProfile.

Motivation
Currently, there is no intermediate tool in the standard library to measure how many times a function is called and its total/average execution time. This is especially useful for university projects, auditing code, and optimizing algorithms during development.

Specification
The decorator adds a .get_stats() method to the wrapped function, which returns an object containing:

calls: Integer count of invocations.

total_time: Total seconds spent in the function using time.perf_counter().

average_time: The average duration per call.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions