© Joey Bernard 2016

Joey Bernard, Python Recipes Handbook, 10.1007/978-1-4842-0241-8_14

14. Testing and Debugging

Joey Bernard

(1)Fredericton, New Brunswick, Canada

This book has focused on giving you tips and tricks on how to write Python code. Very little space has been spent on making this code as good as it could be. In this chapter, you will look at techniques to analyze your code's performance. You will also look at ways to debug your program when errors and mistakes creep into the code you are writing.

14-1. Timing a Section of Code

Problem

You want to time a section of code to see how long it takes to run.

Solution

The Python standard library includes a package named timeit that can run code multiple times and get average runtimes.

How It Works ...

Get Python Recipes Handbook: A Problem-Solution Approach now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.