Profiler

Time Profiler

The time profiler decorator measures the execution time of a function, including any sub-functions that the main function calls. It allows users to specify a file name or complete file path to store the profiling results. The output includes the total time taken by the function, which can be useful for understanding performance bottlenecks in the code. By applying this decorator to any function, developers can easily track its runtime and optimize accordingly.

def inner_function_1():
    # Simulating some work by counting
    total = 0
    for i in range(500000):
        total += i
    return total

def inner_function_2():
    # Simulating some different work, like squaring numbers
    total = 0
    for i in range(10000000):
        total += i * i
    return total

@time_profiler('outer_function_profile.txt')
def outer_function():
    result_1 = inner_function_1()
    print(f"Result from inner_function_1: {result_1}")

    result_2 = inner_function_2()
    print(f"Result from inner_function_2: {result_2}")

# Call the outer function
outer_function()
Result from inner_function_1: 124999750000
Result from inner_function_2: 333333283333335000000

outer_function_profile.txt

         67 function calls in 1.362 seconds

   Ordered by: cumulative time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    1.362    1.362 /tmp/ipykernel_156766/1515566242.py:15(outer_function)
        1    1.303    1.303    1.303    1.303 /tmp/ipykernel_156766/1515566242.py:8(inner_function_2)
        1    0.058    0.058    0.058    0.058 /tmp/ipykernel_156766/1515566242.py:1(inner_function_1)
        2    0.000    0.000    0.001    0.000 {built-in method builtins.print}
        4    0.000    0.000    0.001    0.000 /home/user/miniconda3/lib/python3.11/site-packages/ipykernel/iostream.py:655(write)
        4    0.000    0.000    0.000    0.000 /home/user/miniconda3/lib/python3.11/site-packages/ipykernel/iostream.py:577(_schedule_flush)
        2    0.000    0.000    0.000    0.000 /home/user/miniconda3/lib/python3.11/site-packages/ipykernel/iostream.py:259(schedule)
        2    0.000    0.000    0.000    0.000 /home/user/miniconda3/lib/python3.11/site-packages/zmq/sugar/socket.py:621(send)
        2    0.000    0.000    0.000    0.000 /home/user/miniconda3/lib/python3.11/threading.py:1185(is_alive)
        4    0.000    0.000    0.000    0.000 /home/user/miniconda3/lib/python3.11/site-packages/ipykernel/iostream.py:505(parent_header)
        4    0.000    0.000    0.000    0.000 /home/user/miniconda3/lib/python3.11/site-packages/ipykernel/iostream.py:550(_is_master_process)
        2    0.000    0.000    0.000    0.000 /home/user/miniconda3/lib/python3.11/threading.py:1118(_wait_for_tstate_lock)
        2    0.000    0.000    0.000    0.000 /home/user/miniconda3/lib/python3.11/site-packages/ipykernel/iostream.py:138(_event_pipe)
        2    0.000    0.000    0.000    0.000 {method 'acquire' of '_thread.lock' objects}
        4    0.000    0.000    0.000    0.000 {built-in method posix.getpid}
        4    0.000    0.000    0.000    0.000 {built-in method builtins.isinstance}
        4    0.000    0.000    0.000    0.000 {method 'get' of '_contextvars.ContextVar' objects}
        4    0.000    0.000    0.000    0.000 {method '__exit__' of '_thread.RLock' objects}
        4    0.000    0.000    0.000    0.000 {method 'write' of '_io.StringIO' objects}
        2    0.000    0.000    0.000    0.000 /home/user/miniconda3/lib/python3.11/threading.py:568(is_set)
        4    0.000    0.000    0.000    0.000 {built-in method builtins.len}
        4    0.000    0.000    0.000    0.000 {method 'items' of 'dict' objects}
        1    0.000    0.000    0.000    0.000 /home/user/miniconda3/lib/python3.11/site-packages/dateutil/tz/tz.py:74(utcoffset)
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
        2    0.000    0.000    0.000    0.000 {method 'append' of 'collections.deque' objects}