Benchmarking Short Code Snippets Using timeit
00:00
In this lesson, you’ll use timeit
on a function to see the average time it takes for it to execute. timeit
is a built-in utility module that measures the execution time of small code snippets by running them multiple times to minimize the effects of system noise and provide reliable timing results. To test out timeit
and other tools you’ll be working with, this function fib()
is a recursive function that calculates the nth element of the Fibonacci sequence.
00:33
The Fibonacci function is a great function to profile because it has recursive depth and repeated calls. Since fib()
calls itself twice for every value greater than one, the number of calls grows exponentially.
00:48 So it makes sense to want to profile such a function.
00:52
Okay, now it’s time to write some code. Use timeit
to calculate the average time it takes to execute fib(30)
.
01:02
Here you have a Python file called timeit_fib.py
. You already have your fib()
function that you want to test out here in lines three and four.
01:13
The first thing you need to do is to import timeit
from the timeit
module. So from timeit import timeit
. As you learned before, timeit
executes a function a number of times, so you need to pick a number like iterations
equal to one hundred.
01:36
Now you can actually use timeit
. So let’s create a variable total_time = timeit(
You need to pass in the function you want to test out.
01:53
make sure you’re passing it as a string. This is because timeit
uses strings to run your function in a clean and separate environment. This prevents your existing variables from interfering with timing results, and it ensures accurate benchmarks.
02:11
Now you want to tell timeit
how many times you want it to run this function. So number=iterations
and you also need to set globals=
globals()
.
02:27
You need to do this because timeit
runs your code in an isolated environment and doesn’t automatically have access to your functions or variables. By passing in globals=globals()
, you give it access to your current global scope so it can find what it needs to run.
02:46
Finally, you need to print out total_time
so let’s do that: print(f
"Average time is {
total_time /
the number of iterations.
03:06
You can format with two digits after the decimal point so .2f
, and your average time is going to be in seconds. So let’s add in "seconds"
.
03:20
Okay, now let’s go ahead and run this. So python3 timeit_
fib.py
.
03:39
Okay, it took a few seconds. timeit
was running the fib()
function a hundred times. Now you have the results. Average time is 0.09 seconds.
03:55
Now you know how long your fib()
function takes to run, but some important questions are still unanswered. For example, what is the total number of function calls and out of the total number, how many are recursive and how many are primitive, meaning non-recursive?
04:15
So far, you know that timeit
called fib()
a hundred times, but since fib()
calls itself, you don’t know how many function calls actually happen under the hood.
04:27
How can you answer these questions? Well, one of the tools that answers these questions is cProfile
. In the next lesson, you’ll be learning what it is and how to use it.
Become a Member to join the conversation.