The Python time module is a standard library used to access and convert time, providing functions to get the current time, format strings, measure execution duration, and pause execution. It operates primarily using Unix timestamps, which represent the number of seconds (or nanoseconds) elapsed since the epoch on January 1, 1970, 00:00:00 UTC.

Key functions within the module include:

  • time.time(): Returns the current time as a floating-point number of seconds since the epoch.

  • time.perf_counter(): Measures elapsed time with high precision, ideal for benchmarking code performance.

  • time.sleep(seconds): Suspends the execution of the current thread for a specified number of seconds.

  • time.ctime(): Converts a time in seconds since the epoch into a human-readable string representing local time.

  • time.strftime() and time.strptime(): Format time tuples into strings or parse strings back into time tuples using specific directives.

  • time.monotonic(): Returns the value of a monotonic clock that cannot go backward, ensuring reliable time intervals.

To use these functions, you must first import the module with import time. For example, getting the current time in seconds and converting it to a readable string looks like this:

import time

# Get current time in seconds since epoch
current_seconds = time.time()

# Convert to a readable local time string
readable_time = time.ctime(current_seconds)

print(f"Seconds: {current_seconds}")
print(f"Readable: {readable_time}")

While time handles low-level timestamps and system time, the datetime module is often preferred for working with dates, formatting, and time zones in a more object-oriented manner. The time module is essential for tasks like benchmarking, scheduling delays, and generating logs with precise timestamps.

🌐
Python
docs.python.org › 3 › library › time.html
time — Time access and conversions
The epoch is the point where the time starts, the return value of time.gmtime(0).
🌐
Real Python
realpython.com › python-time-module
A Beginner’s Guide to the Python time Module – Real Python
October 21, 2023 - First, time.time() returns the number of seconds that have passed since the epoch. The return value is a floating point number to account for fractional seconds: ... The number you get on your machine may be very different because the reference ...
🌐
Tutorialspoint
tutorialspoint.com › python › python_date_time.htm
Python - Date and Time
An object of date class is nave, whereas time and datetime objects are aware. A date object represents a date with year, month, and day. The current Gregorian calendar is indefinitely extended in both directions. ... If the value of any argument outside those ranges is given, ValueError is raised. from datetime import date date1 = date(2023, 4, 19) print("Date:", date1) date2 = date(2023, 4, 31) ... Date: 2023-04-19 Traceback (most recent call last): File "C:\Python311\hello.py", line 8, in <module> date2 = date(2023, 4, 31) ValueError: day is out of range for month
🌐
Programiz
programiz.com › python-programming › datetime › current-time
Python Get Current time (With Examples)
Using datetime.strftime() function, we then created a string representing current time. In Python, we can also get the current time using the time module.
🌐
Tutorialspoint
tutorialspoint.com › python › time_time.htm
Python time time() Method
The Python time time() method returns the current UTC time. This return value is obtained as a floating point number expressed in seconds since the epoch. This method does not accept any arguments, hence, it always returns the current UTC time.
🌐
Programiz
programiz.com › python-programming › time
Python time Module (with Examples)
In Python, the time() function returns the number of seconds passed since epoch (the point where time begins).
🌐
Python documentation
docs.python.org › 3 › library › datetime.html
datetime — Basic date and time types
>>> import datetime as dt >>> delta = dt.timedelta( ... days=50, ... seconds=27, ... microseconds=10, ... milliseconds=29000, ... minutes=5, ... hours=8, ... weeks=2 ... ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10) ... import datetime as dt instead of import datetime or from datetime import datetime to avoid confusion between the module and the class. See How I Import Python’s datetime Module.
🌐
W3Schools
w3schools.com › python › ref_module_time.asp
Python time Module
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... import time start = time.time() print(f'Start time: {start}') time.sleep(1) end = time.time() print(f'Elapsed: {end - start:.2f} seconds') Try it Yourself »
🌐
freeCodeCamp
freecodecamp.org › news › python-get-current-time
Python Get Current Time
July 13, 2022 - With the datetime and time modules of Python, you can get the current date and time, or the date and time in a particular time zone.
Find elsewhere
🌐
Real Python
realpython.com › ref › stdlib › time
time | Python Standard Library – Real Python
It allows you to work with time in a variety of ways, including getting the current time, measuring execution durations, and working with time in seconds since the epoch (January 1, 1970, 00:00:00 UTC).
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-time-module
Python Time Module - GeeksforGeeks
July 23, 2025 - In this article, we will discuss the time module and various functions provided by this module with the help of good examples. As the name suggests Python time module allows to work with time in Python. It allows functionality like getting the ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › get-current-date-and-time-using-python
How to Get Current Date and Time using Python - GeeksforGeeks
October 25, 2025 - Using the datetime module, you can fetch local time, UTC, or time in specific time zones, and format it as needed.
Top answer
1 of 16
2637

Use time.time() to measure the elapsed wall-clock time between two points:

import time

start = time.time()
print("hello")
end = time.time()
print(end - start)

This gives the execution time in seconds.


Another option since Python 3.3 might be to use perf_counter or process_time, depending on your requirements. Before 3.3 it was recommended to use time.clock (thanks Amber). However, it is currently deprecated:

On Unix, return the current processor time as a floating point number expressed in seconds. The precision, and in fact the very definition of the meaning of “processor time”, depends on that of the C function of the same name.

On Windows, this function returns wall-clock seconds elapsed since the first call to this function, as a floating point number, based on the Win32 function QueryPerformanceCounter(). The resolution is typically better than one microsecond.

Deprecated since version 3.3: The behaviour of this function depends on the platform: use perf_counter() or process_time() instead, depending on your requirements, to have a well defined behaviour.

2 of 16
1222

Use timeit.default_timer instead of timeit.timeit. The former provides the best clock available on your platform and version of Python automatically:

from timeit import default_timer as timer

start = timer()
# ...
end = timer()
print(end - start) # Time in seconds, e.g. 5.38091952400282

timeit.default_timer is assigned to time.time() or time.clock() depending on OS. On Python 3.3+ default_timer is time.perf_counter() on all platforms. See Python - time.clock() vs. time.time() - accuracy?

See also:

  • Optimizing code
  • How to optimize for speed
🌐
InfluxData
influxdata.com › home › python time module: a how-to guide | influxdata
Python Time Module: A How-To Guide | InfluxData
March 24, 2023 - You don’t have to install it ... as os). The time module provides a range of functions that allow users to perform tasks such as formatting strings, calculating durations, dealing with time zones, and more....
🌐
W3Schools
w3schools.com › python › python_datetime.asp
Python Datetime
The datetime() class also takes parameters for time and timezone (hour, minute, second, microsecond, tzone), but they are optional, and has a default value of 0, (None for timezone).
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-time-time-method
Python | time.time() method - GeeksforGeeks
August 28, 2019 - This module comes under Python’s standard utility modules. time.time() method of Time module is used to get the time in seconds since epoch. The handling of leap seconds is platform dependent.
🌐
iO Flood
ioflood.com › blog › python-time
Python Time Module: Comprehensive Guide
February 7, 2024 - In this example, we measure the execution time of two functions and compare them. This way, you can determine which function is more efficient. Python’s time module represents time in tuples as well. These tuples are handy when you want to display time in a more human-readable format or when you want to extract specific details like year, month, day, hour, minute, etc.
🌐
Built In
builtin.com › articles › timing-functions-python
Timing Functions in Python: A Guide | Built In
We’ve covered four of the most common methods in this guide: using the time module, the timeit module and profiling tools. Now, it’s your turn to put this knowledge into practice. Try timing some Python functions using the different methods we’ve discussed, and see how they compare.
🌐
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...
🌐
IONOS
ionos.com › digital guide › websites › web development › python time module subtitle-h1: what is python’s time module?
What is Python’s time module? - IONOS
May 27, 2025 - The Python time module is essential for im­ple­ment­ing and handling time data in the pro­gram­ming language. With this module, it’s possible to display the current time, format dates and times, and specify a timeframe for running a function.