To get the elapsed time in seconds, you can use timeit.default_timer():

import timeit
start_time = timeit.default_timer()
# code you want to evaluate
elapsed = timeit.default_timer() - start_time

timeit.default_timer() is used instead of time.time() or time.clock() because it will choose the timing function that has the higher resolution for any platform.

Answer from Andrew Clark on Stack Overflow
🌐
Real Python
realpython.com › python-timer
Python Timer Functions: Three Ways to Monitor Your Code – Real Python
December 8, 2024 - 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.
🌐
Python
docs.python.org › 3 › library › timeit.html
timeit — Measure execution time of small code snippets
Source code: Lib/timeit.py This module provides a simple way to time small bits of Python code. It has both a Command-Line Interface as well as a callable one. It avoids a number of common traps fo...
🌐
GitHub
github.com › illagrenan › block-timer
GitHub - illagrenan/block-timer: Measure execution time of your code blocks in Python.
from block_timer.timer import Timer with Timer(): pass # Some operation # Total time ... seconds will be printed · If you have multiple blocks of code, you can set title attribute:
Starred by 9 users
Forked by 5 users
Languages   Python 100.0% | Python 100.0%
🌐
PyPI
pypi.org › project › block-timer
block-timer
JavaScript is disabled in your browser · Please enable JavaScript to proceed · A required part of this site couldn’t load. This may be due to a browser extension, network issues, or browser settings. Please check your connection, disable any ad blockers, or try using a different browser
🌐
PYnative
pynative.com › home › python › python datetime › python measure the execution time of a program
Python Get Execution Time of a Program [5 Ways] – PYnative
February 23, 2022 - Measure the total time elapsed to execute the code block in seconds, milliseconds, minutes, and hours · Also, get the execution time of functions and loops. In this article, We will use the following four ways to measure the execution time in Python: –
🌐
Built In
builtin.com › articles › timing-functions-python
Timing Functions in Python: A Guide | Built In
Timeit can quickly return the time it takes for small blocks of code to run, making it useful for comparing different approaches.
🌐
YouTube
youtube.com › watch
Python - How to get the run time for a block of code - YouTube
In this video we will cover the importance of code run time (execution time) to data analytics, data science and computer programming. Then we will cover ho...
Published   April 3, 2022
🌐
Towards Data Science
towardsdatascience.com › home › latest › did you know – python tips
Did You Know - Python Tips | Towards Data Science
January 18, 2025 - By making a small modification to %timeit magic command, replacing single percentage (%) with double percentage (%%), one can profile a complete code block.
Find elsewhere
🌐
Medium
medium.com › @tubelwj › python-timeit-boost-your-codes-speed-by-100-times-6727d55108b9
Python Timeit: Boost Your Code’s Speed by 100 Times | by Gen. Devin DL. | Medium
October 26, 2023 - In this example, we define a code_block string that contains a simple for loop. We use the `timeit.timeit` function to test the execution time of this code block. The `timeit.timeit` function takes two arguments: the code block to test and the number of times to run the test.
🌐
Towards Data Science
towardsdatascience.com › home › latest › execution times in python
Execution Times in Python | Towards Data Science
March 5, 2025 - This is the preferred way to time execution. When executing code in Python, the CPU does not yield all of its time to the executing process. There are other processes at an OS level that compete for such time. In Python, we can explicitly tell the CPU to sleep the process thread by using time.sleep.
🌐
Vultr
docs.vultr.com › python › examples › measure-the-elapsed-time
Python Program to Measure the Elapsed Time | Vultr Docs
December 10, 2024 - from datetime import datetime start = datetime.now() # Example operation time.sleep(1) end = datetime.now() elapsed = end - start print(f"Elapsed Time using datetime: {elapsed}") Explain Code · This snippet makes use of datetime.now() which returns the current local date and time. The resultant elapsed time is a datetime.timedelta object, which provides the difference in a more readable form compared to raw seconds. Measuring elapsed time in Python can be achieved through various methods, each suitable for different scenarios and precision requirements.
🌐
AskPython
askpython.com › home › python timeit module
Python timeit Module - AskPython
August 6, 2022 - The Python timeit module is a simple interface to quickly measure the execution time for small blocks of code.
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-check-the-execution-time-of-python-script
How to check the execution time of Python script ? - GeeksforGeeks
April 26, 2023 - Python timeit module runs your snippet of code n number of times (the default value is, 1000000) so that you get the statistically most relevant measurement of code execution time.
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-measure-elapsed-time-in-python
How to Measure Elapsed Time in Python - GeeksforGeeks
April 23, 2025 - time.perf_counter() provides the ... time or execution time of a block of code in nanoseconds, we can use the time.time_ns() function....
🌐
LearnDataSci
learndatasci.com › solutions › python-timer
Create a Timer in Python: Elapsed Time, Decorators, and more – LearnDataSci
Since we have roughly established how to calculate the elapsed time, we can refine our code a little. Python's time module offers us alternatives to the time() function, all reporting the time in seconds but in different ways.
🌐
Jakob-bagterp
jakob-bagterp.github.io › timer-for-python
The Easy Way to Start, Stop and Measure Time - Timer for Python ⏳
Simply wrap the Timer around a block of code that you want to measure: After timer.stop(), the elapsed time will be printed in the terminal: