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 =0for i inrange(500000): total += ireturn totaldef inner_function_2():# Simulating some different work, like squaring numbers total =0for i inrange(10000000): total += i * ireturn 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 functionouter_function()
Result from inner_function_1: 124999750000
Result from inner_function_2: 333333283333335000000