Skip to content

How to Measure Time of Functions#

When you want to measure the performance of a function, use the function decorator.

What Is a Decorated Function?#

The @ preceding a function is called a decorator. Such decoractor wraps a function and gives extra functionality without changing the original function.

The pattern is:

@decorator
def function():
    # Function code

Examples#

Basic Usage#

Use the @function_timer() as decorator to measure the performance of a function. Then the Timer will be triggered each time the function is called, and the clock will stop automatically when the function is finished:

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

How to Get Function and Arguments as Thread Name

If you want to keep track of a function and its arguments for troubleshooting and measuring the performance of a function, it's already handled by the @function_timer() decorator. 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)

This also works with keyword arguments:

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

@function_timer()
def anonymous_last_name(first_name, last_name = "unknown"):
    return f"{first_name} {last_name}"

anonymous_last_name("John")

How it appears in the terminal:

% Elapsed time: 0.12 seconds for thread ANONYMOUS_LAST_NAME(FIRST_NAME='JOHN', LAST_NAME='UNKNOWN')

Custom Thread Name and Decimals#

Similar to customising decimals and thread name for the Timer, this is also possible with the @function_timer() decorator. Simply use the thread and decimals arguments where the custom thread will override the default function name and list of arguments:

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

@function_timer(thread="custom", decimals=5)
def sum_numbers(a, b):
    return a + b

sum_numbers(1, 2)

How it appears in the terminal:

% Elapsed time: 0.12345 seconds for thread CUSTOM