Measure Your Code's Execution Time
In the previous lesson, you ran
time.sleep(), but you were wondering, “Were those actually 3 seconds?” You decided it’s time for a test. You can run a test directly from the Python terminal with the
timeit module. So you’re going to call Python with the module
timeit and then pass it a number of times that you want to loop over this, the code that you’re specifying.
Then you give it a piece of code, and Python is going to measure the execution time for this and give you some helpful output. So, let’s hop over to the terminal and then run this code. So I’m going to say
-m (dash module),
I’m going to say
3 times, and then I will pass the code that is
"import time; time.sleep(3)". This is exactly the same code that you used before.
00:53 And now, after you press Enter with this, you might expect it to take, what, 9 seconds? Let’s see what’s actually going to happen. And don’t get nervous when those 9 seconds pass without your code finishing.
01:07 Let’s just give it time. I assure you, what’s happening is what’s supposed to happen, and this is one of those moments where your code is sleeping, so you could just relax a bit yourself, maybe take a deep breath.
01:26 Is there nice weather where you are right now?
01:31 Maybe dream a little about what you might be doing after this, after watching this video course. Well, you’ll get some good suggestions of what you can do next. For now, just keep staring blankly at the screen until you get some output from Python, and here it is. Okay, so this took way longer than 3 times 3 seconds, right? You might’ve expected it to take 9 seconds, but it didn’t. Let’s see why this happened and what this output tells you.
So you ran the
timeit module, and you passed it an argument of
3 loops. So this
3 loops output that you’re seeing here is related to the number that you passed in here. Then you have the code. And the code—you know that it’s supposed to take
3 seconds, so we see this
3 sec per loop relates to the argument in here.
But then here’s another piece, and this is the solution to the riddle of why it took so much longer. The
timeit module actually runs your piece of code not just
n times, but it also gets the output of the best of 5 runs.
Let’s do a quick calculation. So instead of doing
3 loops times
3 seconds—so it does that, but then it also does it
which comes to an amount of
45 seconds, which is approximately the time that you just got to take a break for.
All right, so we see that the
time.sleep() function actually works as expected. You’re passing in
3 seconds and the execution time also takes
Now, using this
n that you saw, it’s actually really important that you specify it to use
3, because the default
n is actually a million.
So if you wouldn’t pass
3, as you did in here, then Python would run a million loops for this code, which would give you a nice time to relax, yes, but that might actually be too long because that means you could sleep for approximately half a year. All right, so after you timed
timeit, it’s now time to move on and build your uptime bot.
Become a Member to join the conversation.