Search⌘ K

Functions of profilehooks

Explore how to use the profilehooks package to profile Python functions and measure their runtime. This lesson guides you through installing profilehooks, applying its decorators for detailed function profiling, and understanding the output. You will also see how profilehooks compares to built-in modules like cProfile and timeit, enabling you to benchmark your code effectively and identify performance bottlenecks.

The last 3rd party package that we will look at in this chapter is called profilehooks. It is a collection of decorators specifically designed for profiling functions.

Let’s start with profilehooks

To install profilehooks, just do the following:

Javascript (babel-node)
pip3 install profilehooks

Now that we have it installed, let’s re-use the example from the last lesson and modify it slightly to use profilehooks:

Python 3.5
# profhooks.py
from profilehooks import profile
#@profile
def mem_func():
t_range= 1000
import random
range_num1 = [random.randint(1,10) for i in range(t_range)]
range_num2 = [random.randint(1,10) for i in range(t_range)]
sum_range = [range_num1[i]+range_num2[i] for i in range(t_range)]
total_of_ranges = sum(sum_range)
print(total_of_ranges)
if __name__ == "__main__":
mem_func()

Testing the implementation using

...