Hell no!
Figuring out how much time it takes to run a specific piece of code is hard!
One of the issues with benchmarking is that your PC is doing OTHER things while also running your code, right?
So, if you just ran your code once, what could happen?
Figuring out how much time it takes to run a specific piece of code is hard!
One of the issues with benchmarking is that your PC is doing OTHER things while also running your code, right?
So, if you just ran your code once, what could happen?
If you got unlucky, a program could hog all of your CPU and your code would appear slow!
So, timeit will run your code a LOT of times and it will count the time it takes to run everything.
There is also another reason timeit will repeat the code.
Can you see what it is?
So, timeit will run your code a LOT of times and it will count the time it takes to run everything.
There is also another reason timeit will repeat the code.
Can you see what it is?
For tiny snippets of code like this, the code runs SO FAST that the computer hardly has precision to time it just once.
So, yeah, those are two main reasons why timeit runs your code a lot of times.
But how many times?
So, yeah, those are two main reasons why timeit runs your code a lot of times.
But how many times?
If you use the timeit CLI, it will try to pick a sensible number of times to run your codeβ¦
But if you are using the Python interface (like I am), it will run your code 1 MILLION times.
That's a lot if you have slow(er) codeβ¦
So, how do you change that number?
But if you are using the Python interface (like I am), it will run your code 1 MILLION times.
That's a lot if you have slow(er) codeβ¦
So, how do you change that number?
So, when you want to time your Python πΒ code, you can use the function timeit.timeit.
Recall that it will run your code (in a string) 1 MILLION times, so use number if you want to run slower code.
And remember that benchmarking is very hard! π―
Recall that it will run your code (in a string) 1 MILLION times, so use number if you want to run slower code.
And remember that benchmarking is very hard! π―
That's it for now! π
If you enjoyed this thread:
πΒ follow me @mathsppblog for more Python πΒ knowledge; and
πΒ retweet the tweet below to share this thread with your audience!
I'll see you around. π
If you enjoyed this thread:
πΒ follow me @mathsppblog for more Python πΒ knowledge; and
πΒ retweet the tweet below to share this thread with your audience!
I'll see you around. π
Loading suggestions...