← Winnitude! Search  ·  Reader Mode (342 ms)
Links: Stay in Reader Retro Site Converter Retro Mode: OFF Original Page ↗
REAL PYTHON · Real Python · 40 LINKS

Python Timer Functions - Real Python

Go to the next lesson

Python Timer Functions

Mahdi Shadkam-Farrokhi 10 Lessons 51m Updated Sep 08, 2026 intermediate python stdlib

A timer is a powerful tool for monitoring the performance of your Python code. By using the time.perf_counter() function, you can measure execution time with exceptional precision, making it ideal for benchmarking. Using a timer involves recording timestamps before and after a specific code block and calculating the time difference to determine how long your code took to run.

In this video course, you'll start with the timer functions in Python's time module and then abstract them into a reusable Timer class. You'll extend that class to track multiple timers, convert it to a data class for convenience, and finally turn it into a context manager so you can time a block of code using a single with statement.

By the end of this course, you'll understand that:

  • time.perf_counter() is the best choice for accurate timing in Python due to its high resolution.
  • process_time() and time() measure different things, so picking the wrong one can skew your results.
  • You can create a custom timer class to encapsulate timing logic and reuse it across multiple parts of your program.
  • Turning your timer into a data class cuts down on boilerplate and makes it easier to configure.
  • You can leverage context managers to neatly measure execution time in specific code blocks, improving both resource management and code clarity.

Along the way, you'll gain deeper insights into how classes , data classes , and context managers work in Python.

What's Included:

  • 10 Lessons
  • Video Subtitles and Full Transcripts
  • Translated Subtitles in 8 Languages
  • 2 Downloadable Resources
  • Accompanying Text-Based Tutorial
  • Interactive Quiz to Check Your Progress
  • 2 Hands-On Coding Exercises
  • Q&A With Python Experts: Ask a Question
  • Certificate of Completion

Downloadable Resources:

Related Learning Paths:

Start Now

Python Timer Functions

8 Lessons 45m

  1. 1. Python Timer Functions (Overview) 00:46
  2. 2. Understanding Python Timers 04:17
  3. 3. Creating a Starter Timer Class 03:35
  4. 4. Exercise: Build a Starter Timer Class 10:00
  5. 5. Managing Multiple Timers 04:28
  6. 6. Enhancing the Timer Class 04:26
  7. 7. Using a Context Manager 06:14
  8. 8. Exercise: Time a Block With a Context Manager 12:00

Reviewing What You've Learned

2 Lessons 6m

  1. 1. Python Timer Functions (Quiz) 05:30
  2. 2. Python Timer Functions (Summary) 00:37
Start Now

About Mahdi Shadkam-Farrokhi

Mahdi is a new member of the Real Python team, creating recordings for video courses and tutorials.

» More about Mahdi

Each tutorial at Real Python is created by a team of developers so that it meets our high quality standards. The team members who worked on this tutorial are:

Aldren

Brenda

Bartosz

Dan

Jaya

Joanna

Kate

Mike

Philipp

Tappan

Geir Arne

Related Courses:

Automating EDA With fg-data-profiling

Using the Claude API in Python

What's New in Python 3.15

← Browse All Courses