Timeit Module - Intermediate Python Programming p.6
Key Takeaways
Uses the timeit module to test snippets of code in Python
Full Transcript
what is going on everybody Welcome to part six of our intermediate Python Programming tutorial Series in this tutorial we're going to be talking about the timeit module so first of all why would you use time it so you can use time well first of all what does time it do time it measures the amount of time it takes for a certain snippet of code to run so why would you use time it over just say um saying you know start equals time.time after you've imported time and then total time um would just be equal to time.time so the current time minus the start boom that's how long it took right well the problem is especially with smaller snippet Snippets of code that's not as that's not precise because there might be a background operation that's just momentarily running that might disrupt that little snippet of code enough to make it appear that that snippet of code took 10 seconds but really it took 10 seconds just because of something else unrelated so what time it's going to do for you is it's actually going to run your snippet of code say 10,000 times depending on the snippet of code of course you can modify it but uh let's say 10,000 times so you get a much more even um and much more um uh statistically relevant sample size basically to to to figure out which one's quicker to run uh let's just see it in action so we're going to start with input list equals uh range of 100 and then we're going to Define that div by five function again uh if you've been following along you might actually already have this so feel free to just sit on your hands and wait for me to write it out so if num modulo 5 equals 0 basically this is just asking um if the number is divisible by five uh return true otherwise return false no problem now let's define a generator that's going to return to XYZ so let's we could say I4 I in input list if div by five I so basically this will be a generator that consists of numbers if they're divisible by five in the range of 100 then we could say um an example of this for like list comprehension might actually be just this this this and this okay so that's really the only difference um this list comprehension this is a generator now what we can do is we can use time it to see which one's faster now real quick let me just show a quick example of time it and then we'll Implement what we just wrote here so let's just uh import time it and then we're just going to print time it. time it um and then time it just runs a string of python code so we could say 1 + 3 and then we say number equals um let's just say a big number like this like I'm doing 500,000 so this is how long 500,000 um examples are going to take so it took this many seconds so not a really long time let's do a few more there we go a little longer anyway you get the idea so to figure out how long each operation ran you can see you would actually divide that time by this number because as we make this number bigger it takes a little bit longer to run so we made this 10 times the size and it appears to be taking didn't we just do 10 times yeah oh no that's legit so 10 times6 is six okay too many tutorials film today I'm doing not doing well Okay cool so um that's just a quick example of the time it um Library Let's test it so let's first test it on like this block of code here okay so I'm just going to highlight all of it copy and then I'm going to just com it out and I'm going to print um time it. time it and then we're going to Triple quote this bad boy paste just the exact paste of that code um and then we're going to say number equals 50 to start I don't know maybe we'll do 5,000 I think this should be a pretty quick operation especially for the generator yeah okay so that's pretty quick right now how how long does it take maybe to make this a to do the list comprehension okay save that run that okay you can see to make the list it took a lot longer like way longer but the real question the other question you might have is okay well that's not really totally fair because this actually built the list so it put the list into memory so how long does that maybe take what if we were to compare those so let's take this and we can convert this um back to a generator here but then we can actually do um we can convert it to a list and in fact I I don't know if we have to I think we could just throw list in front of it but that feels weird let's run that really quick okay and so you can see that converting it to a list while using a generator took slightly longer it's pretty negligible but I think we would probably still always find that the creation of first generation then converting it to a list is about the same um I think I wonder if that would work every time let's do a quick live demo my question is because we're saying list I for I so we're almost removing the whole generator expression sense of it all let me do this and let's bring this back whoops okay so in theory this is like like saying okay that's definitely list comprehension but when we say list this parentheses here is corresponding to list and the generator I don't really know if it's a generator by default like a tupal is by default I think this will work but let's just try so uh for I in XYZ print I and then I'll be curious to know yeah so that worked as as you would expect but I want wonder if on my example where we added two more where now it's a list of the generator for sure does that still work yes it does interesting it's fascinating it's absolutely riveting cool anyway learning new things for myself okay back to the tutorial um so as you can see uh we're pretty much done with the time it we just kind of wanted to show an example but you can pass Snippets of code like this now one thing I will just stress is you have to pass everything that you consider this as like a separate script almost that you're running so when you have like it could have been easy for you to think that you should have been able to get away with this right because um input list is already defined up here and then div by five is defined here but that's not going to work it's going to say those things are um yeah they're not defined even though you're like yes it is but it's not because it's like running in its own kind of process and it doesn't have access to those things so remember um to put all of the code in here and to do that you can just use triple quotes uh to make that happen so um I think that's really all I really wanted to show um for this just uh always remember at least this was just a stupid example of comparing generators to lists of course creating a generator is much faster than creating a list another thing that could do um is compare uh the following so what if we wanted to compare this is getting messy so let's first write the code and then we'll time it below so we've got div by five we've got the generator expression up here let us time the iteration and I'm going to comment this out for now because we're not going to use it right this moment let's time how long it takes to do 4 I in XYZ print I so let's run this code so the creation of a generator and the iteration through that generator so we'll copy this we'll comment it out paste it run it and in fact oh it's going to print it out oh no okay uh let's just uh let's just Define a variable let's just say xals I it's like printing it out to my console it's not on this same same screen okay fascinating it just it went and it tells you how slow printing to console is okay let's try it one more time just cleaning up things okay so this was with the generator and then an iteration through uh just going off of my memory uh this is pretty close to how long it took for us just to build the list now let's do list comprehension so we just change these two things so list compr hension is slightly faster not by a whole lot but let's add some numbers here and okay so this will be list comprehension how many do we add so it should take about 9 seconds probably shouldn't have done it for that long but while we're waiting let's just change this to a generator okay 9.4 seconds and we'll run it one more time so that was for list comprehension now we'll see how long it might take for a generator but again the whole point of a generator isn't to be fast it's to not use memory but that time it appears to actually in all the other cases it was not faster cuz remember this was the generator this was list comprehension this is list comprehension this is a generator interesting but in that case it was faster um it's also probably going to be faster on really huge lists but I I don't really know it it really just depends on what these lists are of how we're building list it's this is a whole whole variety of factors but in most cases at least if you already have the list it's going to be quicker if you can have that list in in your RAM and iterate through that cuz if it's in your RAM it's already there it's ready to go as opposed to a generator it's being truly generated on the fly but uh it's quick to generate versus build an actual list out so interestingly enough that was faster and I think we had a decent sample size so that's kind of interesting but I am recording and I'm doing all kinds of stuff I don't know the uh maybe comment below if you got the same results on that one just fascinating okay that's it for the time it uh module really simple but it's just a good thing to kind of have at your disposal to test and see what's faster in certain exact scenarios that you might come across um in the next tutorial we're going to be talking about enumerate so stay tuned for that if you have any questions comments concerns whatever leave them below otherwise till next time
Original Description
Welcome to part 6 of the intermediate Python programming tutorial series. In this part, we're going to talk about the timeit module.
The idea of the timeit module is to be able to test snippets of code. In our previous tutorial, we were talking about list comprehension and generators, and the difference between the two of them (speed vs memory) was explained. Using the timeit module, I will illustrate this.
https://pythonprogramming.net
https://twitter.com/sentdex
https://www.facebook.com/pythonprogramming.net/
https://plus.google.com/+sentdex
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from sentdex · sentdex · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
Python Encryption Tutorial with PyCrypto
sentdex
Python's Logging Function
sentdex
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
wxPython Programming Tutorial 4: Panels
sentdex
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
wxPython Programming Tutorial 8: Custom Button Images
sentdex
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
Basic PHP Tutorial 12: Associative Array
sentdex
Basic PHP Tutorial 14: Foreach loop
sentdex
Basic PHP Tutorial 16: Include and Require
sentdex
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
Basic PHP Tutorial 4: Variables and Comments
sentdex
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
Basic PHP Tutorial 1: Intro to PHP
sentdex
Basic PHP Tutorial 3: HTML with PHP
sentdex
Basic PHP Tutorial 9: While Loop
sentdex
Basic PHP Tutorial 10: Switch Statement
sentdex
Basic PHP Tutorial 2: Print and Echo
sentdex
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
Basic PHP Tutorial 19: Finding words in strings
sentdex
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
Basic PHP Programming Tutorial 24: String similarity
sentdex
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
Sikuli Tutorial 1: Visually programming in python!
sentdex
Sikuli Tutorial 2: Program visually in python!
sentdex
Sikuli Tutorial 3: Program visually in python!
sentdex
3D Bar Charts in Python and Matplotlib
sentdex
3D Plane wire frame Graph Chart in Python
sentdex
Raspberry Pi Part 1 Introduction
sentdex
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
Raspberry Pi Part 11: Remote Desktop
sentdex
Twitter Analysis: How to rank a user's influence
sentdex
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex
🎓
Tutor Explanation
DeepCamp AI