Skip to content

Function Decorator#

@function_timer()#

Function decorator to measure the performance of a function.

Parameters:

Name Type Description Default
thread str | None

Option to start new thread. By default, the thread name is the function name.

None
decimals int | None

Option to define decimals for output. Minimum 0 (for no decimals) and maximum 9. If None, default is 2 decimals. May be overruled in certain cases due to humanised output.

2
Example

Basic usage:

Python
1
2
3
4
5
6
7
from timer import function_timer

@function_timer()
def test_function():
    # Insert your code here

test_function()

How it appears in the terminal:

% Elapsed time: 12.34 seconds for thread TEST_FUNCTION

With custom thread name and decimals:

Python
1
2
3
4
5
6
7
from timer import function_timer

@function_timer(thread="custom", decimals=5)
def test_function():
    # Insert your code here

test_function()

How it appears in the terminal:

% Elapsed time: 0.12345 seconds for thread CUSTOM

The @function_timer automatically names the function and its arguments as thread name. For example:

Python
1
2
3
4
5
6
7
from timer import function_timer

@function_timer()
def sum_numbers(a, b):
    return a + b

sum_numbers(1, 2)

How it appears in the terminal:

% Elapsed time: 0.12 seconds for thread SUM_NUMBERS(A=1, B=2)