Have you ever tried to optimise your Python 🐍 code, but weren't able to tell which alternative implementation was faster?

Let me teach you how to use the `timeit.timeit` method to measure execution time of small code snippets.

A thread πŸ‘‡πŸ§΅
If you have a snippet of code you want to time, you can use the command line to measure its execution time.

For example, how long does it take for Python 🐍 to add up the first 10,000 integers?

Using the command `python -m timeit`, I get that it takes 189 microseconds.
The execution of `python -m timeit` reports some more info:

β€œ2000 loops, best of 5: ...”

I'll explain what that means, but first let me show you how to use `timeit` in your code.

(Oh, btw, there's a MUCH better way to add consecutive integers! πŸ‘‡)
Oh, did I tell you that the `timeit` module is in the standard library? No extra installs needed.

`timeit.timeit` accepts the piece of code you want to time and returns the number of seconds it took to run.

In the REPL, summing the first 10,000 integers takes... 259 seconds⁉
That's only because the `timeit` function accepts a keyword argument `number`, that specifies the number of times your code is going to be ran.

The default value is 1,000,000 (one million) which can be quite huge!

If you want to time things that take longer, change the default!
So, when we used the CLI with `python -m timeit ...` and the program said

β€œ2000 loops, ...”

what that meant was that `number` was set to `2000`.

Let's try setting `number` to 2000 ourselves and compare the CLI result with the REPL result πŸ‘‡
The REPL clocked 0.51s for 2000 repetitions, giving 255Β΅s to run `sum(range(10_000))`.

255Β΅s is 35% times slower than 188Β΅s!

Why such a big difference?

That's why the CLI includes the β€œ..., best of 5: ...”!

Your PC has many processes running that impact performance...
To try and work around that, the CLI ran the whole experiment a total of 5 times and took the fastest time.

We take the fastest time (and not the mean) because we can interpret that value as the experiment where the other processes interfered less.

Is this making sense, so far?
So, if we want to reproduce the CLI results better, we need to run the REPL experiment 5 times and look for the best result.

Now, the fastest repetition clocked in at 0.393s for 2000 reps, giving ~197Β΅s for the snippet...

197Β΅s is much closer to 188Β΅s!
Alternatively, we can use the `timeit.repeat` method and make use of the `repeat` keyword argument.

The `repeat` method will return a list with all the times, it is then up to you to compute the `min` of that list.
I hope all of this is making sense so far.

Up to this point, you've learned how to use `timeit` from the command line, you've learned about `timeit.timeit(code, number=1000000)` and you've learned about `timeit.repeat(..., repeat=5)`.

Let's explore `timeit.timeit` a bit more.
Suppose you want to see how fast the built-in factorial function, `math.factorial`, is.

For that, let's just use `timeit.timeit` πŸ‘‡

Why do we get a `NameError`??

Because `timeit` runs the snippet in a blank environment, akin to a `.py` script that ONLY contains the snippet.
Ah, no problem, this is easy to fix.

Especially if you know that Python 🐍 can use semicolons `;` to separate statements, like I've shown you here πŸ‘‡

We just need to import the factorial before we use it and then call it...

Problem solved!
But wait!

Now the `timeit` function is _also_ timing the import statement, which is NOT what we want!

That's why the methods `timeit` and `repeat` accept a `setup` keyword argument.

`setup` specifies a piece of code that prepares the environment, e.g. with necessary imports.
Making proper usage of the `setup` keyword argument lets you measure time execution more accurately.

By removing a simple `import` statement, the time measured decreased by 24% and now represents a more accurate measurement.

Nicely done!
Now we want to compare the built-in `math.factorial` to my home-made `fact` function, using `reduce` and `operator.mul`.

First, a quick check showing that my home-made `fact` produces the correct results:
Now we can time the function, we just need to build the proper setup string...

But that's going to be a huge string!

Wouldn't it be nice if you could just tell `timeit` where to look for the `fact` name..?

You can!

That's what the `globals` argument is for πŸ‘‡
In general, you can save yourself some trouble if you use the `globals()` Python 🐍 built-in, which is a dictionary that contains all the global names that Python knows of that are _not_ built-ins.

This makes it more convenient to specify many objects:
These are just some of the cool things you can do with the standard module `timeit`!

Recap:

πŸ‘‰ use `python -m timeit <code>` to measure execution time of a simple snippet in the CL
πŸ‘‰ use `timeit.repeat` to mimic the CLI behaviour
πŸ‘‰ use `timeit.timeit` for one-off measurements
`.repeat` and `.timeit` accept some keyword arguments:

πŸ‘‰ `number`: how many times to run the code
πŸ‘‰ `setup`: set up the environment
πŸ‘‰ `globals`: dict with names needed (useful: the `globals()` built-in)
πŸ‘‰ `.repeat` also accepts `repeat` to produce more accurate measurements
I hope you enjoyed this thread, a LOT of effort was put into this!

If you did, follow me (@mathsppblog) for more content like this.

Consider sharing the beginning of the thread to share your support and appreciation for my work and for Python 🐍!

If you would like to level up your Python 🐍 game, you should definitely subscribe to my newsletter πŸ“¬, to receive gold nuggets of Python knowledge 🧠

You also get to know, first hand, when I publish my in-depth Python articles on my blog.

Subscribe:

mathspp.com/subscribe

β€’ β€’ β€’

Missing some Tweet in this thread? You can try to force a refresh
γ€€

Keep Current with Rodrigo πŸπŸ“

Rodrigo πŸπŸ“ Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @mathsppblog

9 Sep
Have you ever had a hard time debugging 🐞 a Python 🐍 script?

If you say you haven't, you're lying! πŸ˜†

Let me show you something that you're gonna love me for!

Run your script with the `-i` flag!

By doing so, you get a REPL when the script finishes, EVEN with errors πŸ’£

1/4
Let me show you an example.

Below is a piece of code I wrote in a script called `foo`.

If you look closely, you'll see I'm generating 100 random integers...

All of them between 0 and... 0! So I'll generate 100 zeroes (my bug).

Later, a 0/0 division will throw an error.

2/4
When I run the code with the `-i` flag, the terminal shows the error and immediately opens a REPL!

From within the REPL I can inspect my variables and figure out what went wrong!

Isn't this amazing ⁉

3/4
Read 5 tweets
8 Sep
Did you know that Python 🐍 supports keyword argument unpacking?

What's that?

If you have a function that accepts a series of arguments: `arg1`, `arg2`, ...

And if you have a dictionary with keys named `arg1`, `arg2`, ...

Then you can use the dict's values as arguments: Image
The `**` in front of `kwargs` does the unpacking in the function call.

`my_func(kwargs)` – this calls `my_func` with a single argument, the `kwargs` variable (a dictionary in the image above)

...
`my_func(**kwargs)` – this calls `my_func` with one named argument for each (key, value) pair in the dictionary.

E.g., in the above,

`my_func(**kwargs)` unpacks to `my_func(a=73, b="hey!", c=(1, 10))`.
Read 4 tweets
7 Sep
@svpino Hey Santiago! I'd just like to clarify something for the readers of this thread:

The formula

P(A or B or C) = P(A) + P(B) + P(C) - P(A and B and C) is NOT β€Ό true in general.

The result works here because the events are mutually exclusive and P(A and B and C) = 0

1/
@svpino But the actual general formula is

P(A or B or C) = P(A) + P(B) + P(C) - P(A and B) - P(B and C) - P(A and C) + P(A and B and C)

You can read up on the inclusion-exclusion principle, which explains this: πŸ”— en.wikipedia.org/wiki/Inclusion…

This can also be explained visually:

2/
@svpino Checking the overlaps below πŸ‘‡

The ellipsis (representing the events) can overlap all over the place, and we CANNOT overcount the overlaps!

So, we need to remove the pairwise overlaps:

A and B
A and C
B and C

But when we do that, we over-subtract the final intersection,

3/
Read 4 tweets
2 Sep
Have you ever seen anything like this in Python 🐍 loops??

I just saw this being used for the first time, ever, and I decided to figure out what the heck this was...

And so here goes the explanation πŸ‘‡πŸ§΅

1/
Foreword:

Python has many amazing capabilities to deal with `for` loops, among which you can count the `zip` and `enumerate` built-ins.

This little thread assumes you are familiar with both.

Read these if you need:
πŸ”—Β mathspp.com/blog/pydonts/e…
πŸ”—Β mathspp.com/blog/pydonts/z…

2/
When using `zip` and `enumerate` in your `for` loops, something you do often is to unpack the values you are iterating over.

Unpacking, in and out of itself, is an amazing feature of Python, and you can read about it in the linked article if needed.

mathspp.com/blog/pydonts/d…

3/
Read 10 tweets
31 Aug
πŸ₯‡ I challenged you to write a Python 🐍 function `is_pangram`, that checks if a string contains all 26 letters of the English alphabet.

(Original challenge linked below πŸ‘‡)

I got many amazing suggestions.

Here is a thread 🧡 about different solutions.

Overall, people tended to follow one of 3 approaches:

1. remove irrelevant chars and see if there are 26 letters left (duplicates excluded);

2. check if each letter of the alphabet appears in the string; and

3. check if the alphabet is fully contained inside the input string.
Then, I saw two major trends in the solutions people shared, regardless of strategy.

Either they did all the work by hand with a `for` loop or they leveraged tools like Python's `set`.

Here's the archetype for a `for` based solution: Image
Read 17 tweets
24 Aug
Did you know that Python 🐍 has over 240 modules in the standard library?

Here is a MEGA thread 🧡with a super high level overview of those modules...

... but before that, I challenge you to name as many modules by heart as possible! πŸ’¬πŸ‘‡

Let's see who gets more!
To help get a sense for the more popular and used modules, leave a like β™₯ on the tweets that contain modules you remember using.

Can you guess what module(s) are the most widely used..? πŸ€”

(P.S. To learn more about all these cool modules, check docs.python.org/3/library/)
Let's kick things off with

πŸ‘‰ Text Processing Services
πŸ”— docs.python.org/3/library/text…

1️⃣ string β€” Common string operations

2️⃣ re β€” Regular expression operations

3️⃣ difflib β€” Helpers for computing deltas

4️⃣ textwrap β€” Text wrapping and filling
Read 89 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!

:(