Skip to main content

Time profiler

Project description

tm_profiler

Python - time profiler

Package version >= 3.0.1 supported from Python >= 3.8 .
For Python 2, please use package version 2.x.x.

Developed by Normunds Pureklis (c) 2025

Installation

Install via pip::

$ pip install tm-profiler

Available functions

Imported like: import tm_profiler or import tm_profiler as tp

Function Usage
profile(print_inline=False) Decorator for function time profilig
print_stat(sort_by: TpSort = TpSort.NAME) Print all collected statistic
print_last() Print statistic last collected record
disable() Disable profiler
enable() Enable profiler
reset() Reset profiler
set_output_dec(int) Set profiler output decimal places
set_name_format(name_format: TpNameFormat) Set profiler output function name format

Imported like: from tm_profiler import *

Function Usage
tp_profile(print_inline=False) Decorator for function time profilig
tp_print_stat(sort_by: TpSort = TpSort.NAME) Print all collected statistic
tp_print_last() Print statistic last collected record
tp_disable() Disable profiler
tp_enable() Enable profiler
tp_reset() Reset profiler
tp_set_output_dec(int) Set profiler output decimal places
tp_set_name_format(name_format: TpNameFormat) Set profiler output function name format

Usage

Add decorator to functions which needs to profile.
Run print_stat() function to print time statistic.

import tm_profiler as tp

@tp.profile()
def func_a():
    res = 0
    for _ in range(1000000):
        res = 100 / 10
    return res

@tp.profile()
def func_b():
    res = 0
    for _ in range(1000000):
        res = 100 / 10
    return res

func_a()

for _ in range(3):
    func_b()

tp.print_stat()

Output:

-------------------------------------------------------------
## Time Profiler: #
-------------------------------------------------------------
| Name            | Time total(s) | Calls | Time average(s) |
-------------------------------------------------------------
| main.py[func_a] |        0.0117 |     1 |          0.0117 |
| main.py[func_b] |        0.0317 |     3 |          0.0106 |
-------------------------------------------------------------

To print inline time statistic, use decorator function argument print_inline=True.

import tm_profiler as tp

@tp.profile(print_inline=True)
def func_a():
    res = 0
    for _ in range(1000000):
        res = 100 / 10
    return res

func_a()

Output:

NOTE: Profiler information will be printed before function result is returned!

## TP # Function (main.py[func_a]:1) - took: 0.0105s #
Function result: 10.0

To print time statistic for last function run, use profiler function print_last().

import tm_profiler as tp

@tp.profile()
def func_a():
    res = 0
    for _ in range(1000000):
        res = 100 / 10
    return res

print(f"Function result: {func_a()}")

tp.print_last()

Output:

Function result: 10.0
## TP # Function (main.py[func_a]:1) - took: 0.0146s #

To disable profiler, use profiler function disable().
Statistic will not be collected and printed.
To enable back use function enable().

import tm_profiler as tp

@tp.profile()
def func_a():
    res = 0
    for _ in range(1000000):
        res = 100 / 10
    return res

tp.disable()
print(f"Function result1: {func_a()}")
tp.print_last()

tp.enable()
print(f"Function result2: {func_a()}")
tp.print_last()

Output:

Function result1: 10.0
Function result2: 10.0
## TP # Function (main.py[func_a]:1) - took: 0.0123s #

Use reset() function to reset profiler collected data.

import tm_profiler as tp

@tp.profile()
def func_a():
    res = 0
    for _ in range(1000000):
        res = 100 / 10
    return res

@tp.profile()
def func_b():
    res = 0
    for _ in range(1000000):
        res = 100 / 10
    return res

func_a()

for _ in range(3):
    func_b()

tp.print_stat()

tp.reset()
tp.print_stat()

func_a()
tp.print_stat()

Output:

-------------------------------------------------------------
## Time Profiler: #
-------------------------------------------------------------
| Name            | Time total(s) | Calls | Time average(s) |
-------------------------------------------------------------
| main.py[func_a] |        0.0117 |     1 |          0.0117 |
| main.py[func_b] |        0.0317 |     3 |          0.0106 |
-------------------------------------------------------------
--------------------------------------------------
## Time Profiler: #
--------------------------------------------------
| Name | Time total(s) | Calls | Time average(s) |
--------------------------------------------------
--------------------------------------------------
-------------------------------------------------------------
## Time Profiler: #
-------------------------------------------------------------
| Name            | Time total(s) | Calls | Time average(s) |
-------------------------------------------------------------
| main.py[func_a] |        0.0108 |     1 |          0.0108 |
-------------------------------------------------------------

Use set_output_dec(int) to configure output number decimal places (affects only print output).

import tm_profiler as tp

@tp.profile()
def func_a():
    return 10

func_a()

tp.set_output_dec(6)
tp.print_last()

Output:

## TP # Function (main.py[func_a]:1) - took: 0.014630s #

Use set_name_format(name_format: TpNameFormat) to configure how is stored decorated function name
(Imortant to set before any decorated function is used, as it affects how function name is stored).

import tm_profiler as tp

tp.set_name_format(name_format=tp.TpNameFormat.REL)
# Available options:
#  TpNameFormat.NAME - function name (default)
#  TpNameFormat.REL - function name with relative path
#  TpNameFormat.ABS - function name with absolute path

@tp.profile()
def func_a():
    return 10

func_a()

tp.set_output_dec(6)
tp.print_last()

Sort profiler output data.
Use sort_by parameter for function print_stat() to sort output.

import tm_profiler as tp

@tp.profile()
def func_a():
    return 10

func_a()

tp.print_stat(sort_by=tp.TpSort.CALLS)
# Available options:
#  TpSort.NAME  - sort by name (default)
#  TpSort.CALLS - sort by count of calls
#  TpSort.TOTAL - sort by total time
#  TpSort.AVG   - sort by average time

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tm_profiler-3.0.1.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tm_profiler-3.0.1-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file tm_profiler-3.0.1.tar.gz.

File metadata

  • Download URL: tm_profiler-3.0.1.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for tm_profiler-3.0.1.tar.gz
Algorithm Hash digest
SHA256 0bf8aaf2a59f17b9419a2a13a2b4bc86089467ac88d33c55fe8776b192cfd48b
MD5 7674372cd11ea0304574d4b8a38e9d41
BLAKE2b-256 b43f2fee5203d7d1b7e4af372bdf02951e7aae114a487cfd0b55a123237b7d7b

See more details on using hashes here.

File details

Details for the file tm_profiler-3.0.1-py3-none-any.whl.

File metadata

  • Download URL: tm_profiler-3.0.1-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for tm_profiler-3.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5cb716f31cf23b5cd96147e47fb6fe6661d26c1f7736a2e22df9241d8949da08
MD5 1f280f63948f445327e1ff02982742da
BLAKE2b-256 9de8691260bfc19f34068a29554e449c7f88a43346d6c301e2aff131b2b1f641

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page