---Advertisement---

Python Timer Function: Measure Elapsed Time with Best Examples in Python 2025

By Bhavani

Updated On:

---Advertisement---
Python Timer Function

Python Timer Function: Python’s time module provides efficient tools to manage and measure time-related operations. This module includes various functions for tracking performance, computing elapsed time, and handling date-time objects.

  1. time.time() – Tracks elapsed time in seconds.
  2. time.sleep() – Pauses code execution for a specified time.
  3. time.ctime() – Converts timestamps to readable string format.
  4. time.gmtime() – Displays structured time in UTC format.
  5. time.process_time() – Tracks CPU processing time for precise performance evaluation.
  6. time.thread_time() – Measures active thread execution time.

The time.time() function records the current time in seconds since the Unix epoch (January 1, 1970).

python

import time

start_time = time.time()

print(“Starting timer…”)

time.sleep(2)  # Pause for 2 seconds

end_time = time.time()

elapsed_time = end_time – start_time

print(f”Elapsed time: {elapsed_time} seconds”)

css

Starting timer…

Elapsed time: 2.001 seconds


The time.sleep() function pauses your program for the specified number of seconds.

python

import time

print(“Starting delay…”)

time.sleep(3)

print(“Delay completed!”)

arduino

Starting delay…

Delay completed!


The time.ctime() function converts timestamps into a human-readable string.

python

import time

current_time = time.time()

print(“Current time in human-readable format:”, time.ctime(current_time))

pgsql

Current time in human-readable format: Mon Mar 14 10:30:05 2025


The time.process_time() function is ideal for performance benchmarking.

python

from time import process_time

start_time = process_time()

for i in range(1000000):

    pass

end_time = process_time()

print(“CPU process time:”, end_time – start_time)

arduino

CPU process time: 0.025 seconds


This function tracks the time spent specifically on active thread execution.

python

import time

import threading

def sample_task():

    start_time = time.thread_time()

    for _ in range(10000000):

        pass

    end_time = time.thread_time()

    print(f”Thread execution time: {end_time – start_time}”)

thread = threading.Thread(target=sample_task)

thread.start()

thread.join()

css

Thread execution time: 1.03 seconds

 How to Print Without Newline in Python

Download Python

Leave a Comment

Index