Locked learning resources

You must own this product to watch this lesson.

Locked learning resources

You must own this product to watch this lesson.

Set Speed Test

This lesson is from the Real Python video course by James Uejio.

00:00 In order to see how fast functions take to run, you can use the %timeit method that is built into IPython, which is an interactive Python terminal. So use %timeit and then just put some expression like this. It will run it a bunch of times, find the mean, and then also find the standard deviation on how long it took.

00:23 So for example, it took 7 nanoseconds plus or minus 0.019 nanoseconds per loop. It had 7 runs of, I don’t know, 10 billion loops each, or something like that—10 million loops.

00:35 So this is pretty accurate. It ran it a lot, and this is very, very quick. Let’s see how fast it takes to do some operations on sets, lists, and tuples.

00:46 First, we will just initialize our set.

00:52 We’ll loop through 1000, we’ll add them all to our set. Remember, .add() mutates our set, and then just return s.

01:01 I’ll load this interactively so we have access to this method. We’ll do %timeit like this, wait a little bit.

01:14 And it took 72 microseconds plus or minus 390 nanoseconds, and it ran 7 runs with 10,000 loops each. So here, let’s initialize our list.

01:32 .append(), because lists use .append(). Exit this, reload this, clear, use our %timeit.

01:49 And it took 71 microseconds plus or minus 480. So, let’s do Up arrow—oh, it actually remembered. So, it takes about the same amount of time to initialize a set with a 1,000 numbers, as it does a list.

02:05 Let’s just quickly do it for a tuple, tup = tuple(). So remember, we use += because tuples are immutable and we have to use this little interesting parentheses-comma thing to add tuples.

02:25 Exit, clear, load. This is defined twice. So, basically, I’m using VS Code, which has a package called Python, which will show you if you have any syntax errors and also auto-format when you save. It’s just useful to make your coding a little bit faster.

02:44 Why is this…? There we go. Okay.

02:54 So, we see here that initializing tuples takes longer than initializing sets and lists. Well, it’s mostly because here we are mutating our variables, while here we have to create a new tuple and then reassign it to here and add tuples together.

03:15 That’s a lot harder than just adding to an object that already exists in memory. But that wasn’t the point of this video, just something cool you learn when you start running functions and looking at how fast they take. What we really care about is membership.

03:34 Let’s loop through our numbers again. Well, first let’s define our set. s = initialize_set(), lst = initialize_lst(), tup = initialize_tuple().

03:50 This will happen outside the function, so this will happen when I load the file so it won’t actually be counted when I start timing these methods. Let’s just do i in s. This is sort of an interesting expression here.

04:04 This is literally just like writing like True or False or, like, x * 10 or something. Like, it doesn’t actually do anything, because we’re not assigning it to anything, but it does execute the code.

04:15 So this will actually execute i in s for each number between 0 and 1000, not including 1000. Let’s try membership_lst(), change this to lst, and membership_tuple().

04:36 Eh, we’ll just call it tuple for… what did I call it here? Okay, yeah. This.

04:46 Perfect! Let’s exit this, and now we will %timeit membership_set(). It took 57 microseconds plus or minus 1 microsecond per loop.

05:07 This took 5.7 milliseconds plus or minus 25 microseconds, and it only did 100 loops. I’m assuming that this optimizes, because if it were to do 10,000 loops for everything, this would take way too long.

05:20 But it still gives us a pretty realistic mean and standard deviation, so we can see that checking membership in sets is about a hundred times faster, I believe, which is pretty significant.

05:36 And here we’ll try the tuple. So, the tuple—checking membership in lists and tuples is pretty similar. Checking membership in sets is almost instant, or at least instant relative to lists and tuples, and is much faster.

You must own this product to join the conversation.