Jump to ratings and reviews
Rate this book

Python Benchmarking: Measure The Execution Time Of Python Code With The time And timeit Modules

Rate this book
Without benchmarking, we're working in the dark.

Python code can be slow.

Benchmarking is a way of discovering exactly how long code takes to execute. Without benchmarking, we have no idea whether changes make code run faster or not.

You need to know:

- How to benchmark statements, functions, and programs using the time module.
- How to develop benchmarking helper functions, context managers, and decorators.
- How to benchmark snippets of code using the timeit module.

Benchmarking is required to develop fast Python code.

Python provides 5 built-in functions for reporting the current time. The problem is, that many developers use just one, the time() function, and are unaware of how inappropriate it is for benchmarking. Instead, we should be using the perf_counter() function.

Python also provides the timeit module with API and command line interface specifically designed for benchmarking. It encodes best practices such as repeated execution of target code and use of a high-precision timing function. Nevertheless, few developers use it because it is confusing. The trick is to adopt the "timeit" mindset.

Introducing: "Python Benchmarking". A new book designed to teach you how to bring modern benchmarking practices to your projects, super fast!

You will get fast-paced tutorials showing you how to benchmark your Python code, as well as some much-needed advice on advanced topics, such as:

- How to benchmark asyncio programs and coroutines.
- How to choose the precision and units of measure when reporting benchmark results.
- Why it is a good idea to repeat benchmark tests many times and report average results.
- How profiling is not benchmarking but can help in deciding what to optimize.

Each tutorial is carefully designed to teach one critical aspect of how to effectively benchmark Python code.

Learn Python benchmarking correctly, step-by-step.

378 pages, Kindle Edition

Published October 23, 2023

About the author

Jason Brownlee

47 books77 followers
Jason Brownlee, Ph.D. trained and worked as a research scientist and software engineer for many years (e.g. enterprise, R&D, and scientific computing), and is known online for his work on Computational Intelligence (e.g. Clever Algorithms), Machine Learning and Deep Learning (e.g. Machine Learning Mastery, sold in 2021) and Python Concurrency (e.g. Super Fast Python).

Jason writes fiction under the pseudonym J.D. Brownlee: https://www.goodreads.com/jdbrownlee

Ratings & Reviews

What do you think?
Rate this book

Friends & Following

Create a free account to discover what your friends think of this book!

Community Reviews

5 stars
1 (100%)
4 stars
0 (0%)
3 stars
0 (0%)
2 stars
0 (0%)
1 star
0 (0%)
No one has reviewed this book yet.

Can't find what you're looking for?

Get help and learn more about the design.