Get startedGet started for free

Examining runtime

1. Examining runtime

In this lesson, we'll learn how to examine the runtime of our code. As mentioned in the previous chapter, runtime is an important consideration when thinking about efficiency.

2. Why should we time our code?

Comparing runtimes between two code bases, that effectively do the same thing, allows us to pick the code with the optimal performance. By gathering and analyzing runtimes, we can be sure to implement the code that is fastest and thus more efficient.

3. How can we time our code?

To compare runtimes, we need to be able to compute the runtime for a line or multiple lines of code. IPython comes with some handy built-in magic commands we can use to time our code. Magic commands are enhancements that have been added on top of the normal Python syntax. These commands are prefixed with the percentage sign. If you aren't familiar with magic commands, don't worry, take a moment to review the documentation using the provided link. We won't be using all the magic commands listed in the docs, but it's helpful to know what magic commands are at your disposal.

4. Using %timeit

Consider this example: we want to inspect the runtime for selecting 1,000 random numbers between zero and one using NumPy's random-dot-rand function. Using %timeit just requires adding the magic command before the line of code we want to analyze. That's it! One simple command to gather runtimes. Magic indeed!

5. %timeit output

One advantage to using %timeit is the fact that it provides an average of timing statistics.

6. %timeit output

Notice that the output provides a mean, and standard deviation, of time.

7. %timeit output

We also see that multiple runs and loops were generated. %timeit runs through the provided code multiple times to estimate the code's execution time. This provides a more accurate representation of the actual runtime rather than relying on just one iteration to calculate the runtime. The mean and standard deviation displayed in the output is a summary of the runtime considering each of the multiple runs.

8. Specifying number of runs/loops

The number of runs represents how many iterations you'd like to use to estimate the runtime. The number of loops represents how many times you'd like the code to be executed per run. We can specify the number of runs, using the -r flag, and the number of loops, using the -n flag. Here, we use -r2, to set the number of runs to two and -n10, to set the number of loops to ten. In this example, %timeit would execute our random number selection 20 times in order to estimate runtime (2 runs each with 10 executions).

9. Using %timeit in line magic mode

Another cool feature of %timeit is its ability to run on single or multiple lines of code. When using %timeit in line magic mode, or with a single line of code, one percentage sign is used.

10. Using %timeit in cell magic mode

Similarly, we can run %timeit in cell magic mode (or provide multiple lines of code) by using two percentage signs.

11. Saving output

We can save the output of %timeit into a variable using the -o flag.

12. Saving output

This allows us to dig deeper into the output and see things like the time for each run, the best time for all runs, and the worst time for all runs. Let's try using %timeit with some of Python's built-in data structures.

13. Comparing times

In a previous chapter, we mentioned some of Python's built-in data structures called lists, dictionaries, and tuples. Python allows us to create these data structures using either a formal name, like list, dict, and tuple spelled out, or a shorthand called literal syntax.

14. Comparing times

If we wanted to compare the runtime between creating a dictionary using the formal name and creating a dictionary using the literal syntax, we could save the output of the individual %timeit commands as shown here. Then, we could compare the two outputs.

15. Comparing times

For simplicity, let's look at just the output of both the %timeit commands to see which was faster. Here, we can see that using the literal syntax to create a dictionary is faster than using the formal name without writing code to do the analysis for us.

16. Off to the races!

Now that we've learned how to use %timeit, we can compare different coding approaches to select the most efficient one. It's off to the races!