Skip to main content

Time profiler

Project description

tm_profiler

Python - time profiler

!!!Under construction!!! Beta version! Currently, in testing!

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-0.0.3.tar.gz (9.6 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-0.0.3-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for tm_profiler-0.0.3.tar.gz
Algorithm Hash digest
SHA256 421c6b1c87000f928816f5cc3786b0f24367ebe44e142df35979e58e10d609d3
MD5 e1d6327f6afe7bc764d49fc5e727637f
BLAKE2b-256 afcd3e95498ccb7f187277816c220017a943bf75063b9b30e518156d05bbca1c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tm_profiler-0.0.3-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-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 5f161c2e45f3ae5560813cb192f7559ce54095035e73de357b8beb1946e43a1c
MD5 014e34b5b9320fc109b8992a0c0e675b
BLAKE2b-256 57b4c2299edfdbb248ac2c154712ba1c1105700083f51571fbdba9a2d25d9ea0

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