Skip to main content

Time profiler

Project description

tm_profiler

Python - time profiler

Package version 2.x.x is to support Python 2 (2.7 and newer versions).
For Python >= 3.8, please use package version >= 3.0.1.

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-2.0.1.tar.gz (9.8 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-2.0.1-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for tm_profiler-2.0.1.tar.gz
Algorithm Hash digest
SHA256 edea765b0f81717dc1cb1edd565c591564847f30f4331016eecc1b31e9e4c272
MD5 f6e51e2ae987d32c212d0812361cda5b
BLAKE2b-256 d714a91166b2ed147d683faa36ebd8af6998037b68ba4bb5b39c1fa2e6ec058e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tm_profiler-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 6.6 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-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 be2b5750969f988ac9ba9bea2fa5e6f58d14c284e96c89d6ee81841fd4c93368
MD5 b7480f719b7ba22bbd9f427d2d32a268
BLAKE2b-256 acba5e6a5974caba82e6d3729cbc1b212ca310bd4cf7a9a932181094a67b53b2

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