Package: line-profiler-decorator
line-profiler-decoratorWrapper aroundline-profileradding a few features as an decoratorInstallpipinstallline-profiler-decoratorUsagefromtimeimportsleepfromline_profiler_decoratorimportprofiler@profilerdefslow_function():print("quick line")sleep(1)# slow onesleep(0.3)# not that slowOutputquick line
Timer unit: 1e-06 s

Total time: 6.52823 s
File: /Users/fabio/projects/line-profiler-decorator/examples/example.py
Function: slow_function at line 5

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
     5                                           @profiler(aggregate=True)
     6                                           def slow_function():
     7         5         69.0     13.8      0.0      print("quick line")
     8         5    5016944.0 1003388.8     76.8     sleep(1)  # slow one
     9         5    1511218.0 302243.6      23.1     sleep(0.3)  # not that slowOptionsSave output to a fileSave the output to a file instead of stdout@profiler("output_file.txt")defslow_function():print("quick line")sleep(1)# slow onesleep(0.3)# not that slowAggregate resultsAggregate the results of multiple calls to the decorated function@profiler(aggregate=True)defslow_function():print("quick line")sleep(1)# slow onesleep(0.3)# not that slowdefrun_slow(n=100):for_inrange(n):slow_function()Follow functionfromtimeimportsleepfromline_profiler_decoratorimportprofilerdefother_function():sleep(1)# slow onesleep(0.3)# not that slow@profiler(follow=[other_function])deffunction_that_calls_other_function(n):for_inrange(n):other_function()OutputIn [3]: function_that_calls_other_function(10)                                                                                                                                      
Timer unit: 1e-06 s

Total time: 13.0668 s
File: /Users/fabio/projects/line-profiler-decorator/examples/example.py
Function: other_function at line 17

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
    17                                           def other_function():
    18        10   10037030.0 1003703.0     76.8      sleep(1)  # slow one
    19        10    3029725.0 302972.5     23.2      sleep(0.3)  # not that slow

Total time: 13.0672 s
File: /Users/fabio/projects/line-profiler-decorator/examples/example.py
Function: function_that_calls_other_function at line 22

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
    22                                           @profiler(follow=[other_function])
    23                                           def function_that_calls_other_function(n):
    24        11         97.0      8.8      0.0      for _ in range(n):
    25        10   13067104.0 1306710.4    100.0          other_function()Grouped aggregationsGroup aggregations using a key function.keyis a function that receives the same arguments as the decorated function and returns a value.
The profile results of each call of the decorated function will be grouped and aggregated together by the value returned by thekeyfunctionExample:fromtimeimportsleepfromline_profiler_decoratorimportprofiler@profiler(aggregate=True,key=lambdax:x%2==0)deffunction_with_grouped_aggregations(number):"""This function runs faster for even numbers."""ifnumber%2:# sleep for 1 second if number is oddsleep(1)else:# sleep for 0.2 second if number is evensleep(0.2)sleep(0.5)defrun_function_with_grouped_aggregations(n=10):for_inrange(n):function_with_grouped_aggregations(_)OutputThekeyfunction returns valueTruefor even numbers andFalsefor odd numbers.
That way the aggregated results will be grouped into 2 profile results.Timer unit: 1e-06 s

Total time: 3.533 s
File: /Users/fabio/projects/line-profiler-decorator/examples/example.py
Function: function_with_filtered_aggregations at line 28

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
    28                                           @profiler(aggregate=True, key=lambda x: x % 2 == 0)
    29                                           def function_with_filtered_aggregations(number):
    30                                               """This function runs faster for even numbers."""
    31         5         17.0      3.4      0.0      if number % 2:
    32                                                   # sleep for 1 second if number is odd
    33                                                   sleep(1)
    34                                               else:
    35                                                   # sleep for 0.2 second if number is even
    36         5    1015312.0 203062.4     28.7          sleep(0.2)
    37         5    2517674.0 503534.8     71.3      sleep(0.5)

Timer unit: 1e-06 s

Total time: 7.53658 s
File: /Users/fabio/projects/line-profiler-decorator/examples/example.py
Function: function_with_filtered_aggregations at line 28

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
    28                                           @profiler(aggregate=True, key=lambda x: x % 2 == 0)
    29                                           def function_with_filtered_aggregations(number):
    30                                               """This function runs faster for even numbers."""
    31         5         15.0      3.0      0.0      if number % 2:
    32                                                   # sleep for 1 second if number is odd
    33         5    5017337.0 1003467.4     66.6          sleep(1)
    34                                               else:
    35                                                   # sleep for 0.2 second if number is even
    36                                                   sleep(0.2)
    37         5    2519231.0 503846.2     33.4      sleep(0.5)Looking at the results above we can see that the first block only aggregated call with evennumbertherefore spent no time at line 33. On the other hand, the second block shows the same for odd numbers
where no time was used on even numbers.