How To Use Timeit In Jupyter Notebook

Deep dive on how to use the timeit module in Jupyter Notebook for measuring the execution time of Python code snippets and functions.

As a data scientist, you know that optimizing code performance is crucial for efficient data analysis. One tool that can help you achieve this is timeit, a Python module that measures the execution time of small code snippets. In this blog post, we will explore how to use timeit in Jupyter Notebook, a popular environment for data analysis. Don’t forget you can get free Jupyter notebooks online at Saturn Cloud.

What is timeit?

The timeit module provides a simple way to time small bits of Python code. It has both a command-line interface and a callable one. It avoids a number of common traps for measuring execution times. For example, it automatically disables the garbage collector to prevent timing overheads that are not related to the code being measured.

Installing timeit

Timeit is included in Python’s standard library, so you don’t need to install any additional packages. However, if you’re using a virtual environment, make sure to activate it before using timeit.

Using timeit in Jupyter Notebook

To use timeit in Jupyter Notebook, you need to import the module and create a Timer object. The Timer object takes two arguments: the code you want to time and the number of times you want to repeat it.

Here’s an example:

import timeit

code_to_test = """
def calculate_sum():
    total = 0
    for i in range(1000):
        total += i
    return total

calculate_sum()
"""

elapsed_time = timeit.timeit(code_to_test, number=10000)
print(elapsed_time)

In this example, we define a function that calculates the sum of the first 1000 integers and then call it. We then pass this code snippet to timeit.timeit() along with the argument number=10000, which specifies that we want to repeat the code 10000 times. The timeit function returns the total time it took to execute the code 10000 times.

Measuring execution time of a function

You can also use timeit to measure the execution time of a specific function. To do this, you need to define a function that calls the function you want to time and then pass this wrapper function to timeit.

Here’s an example:

import timeit

def calculate_sum():
    total = 0
    for i in range(1000):
        total += i
    return total

def wrapper():
    calculate_sum()

elapsed_time = timeit.timeit(wrapper, number=10000)
print(elapsed_time)

In this example, we define a wrapper function that calls the calculate_sum() function. We then pass the wrapper function to timeit.timeit() along with the argument number=10000. The timeit function returns the total time it took to execute the wrapper function 10000 times, which includes the time it took to call the calculate_sum() function.

Conclusion

In this blog post, we’ve explored how to use timeit in Jupyter Notebook to measure the execution time of small code snippets and functions. By using timeit, you can easily identify performance bottlenecks and optimize your code for faster data analysis. Remember to use timeit wisely and to always interpret the results in the context of your specific use case. Happy coding!