
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to return the current CPU time in Python?
In this article, we retrieve the current CPU time in Python using the time() method, which is imported from the Python time module.
The time module in Python provides various methods and functions related to time. Here we use the time.The time() method to get the current CPU time in seconds. The time is calculated since the epoch. This method returns a float value that represents the seconds since the epoch.
Epoch is the starting point of time and is platform-dependent. The epoch is January 1, 1970, 00:00:00 (UTC) on Windows and most Unix systems, and leap seconds are not included in the time in seconds since the epoch. We use time.gmtime(0) to get the epoch on a given platform.
The following are the various methods to return the current CPU time in Python by using different functions provided in the time module.
Using time.time() Method
The time.time() method returns the current time in seconds since the epoch (January 1, 1970). It returns a float value and is most commonly used to measure wall-clock time.
Example
The following program demonstrates how to get the current CPU time using time.time(). It also retrieves the epoch using time.gmtime(0) to show the reference point from which the time is measured.
import time obj = time.gmtime(0) epoch = time.asctime(obj) print("The epoch is:", epoch) curr_time = time.time() print("Current CPU time:", curr_time)
Following is the output for the above code:
The epoch is: Thu Jan 1 00:00:00 1970 Current CPU time: 1662371608.0567493
Using time.process_time() Method
The time.process_time() method returns the CPU time used by the current process as a floating-point number in seconds. Unlike time.time(), which measures real (wall-clock) time, time.process_time() focuses on the time spent by the CPU executing the process, excluding time spent waiting for I/O operations or sleep time.
import time start = time.process_time() # Dummy loop to consume CPU cycles for _ in range(1000000): pass end = time.process_time() print("CPU processing time:", end - start)
Following is the output for the above code:
CPU processing time: 0.03125
Using time.thread_time() Method
The time.thread_time() method returns the CPU time consumed by the current thread. It's helpful in multithreaded applications where you want to track the performance of individual threads.
Unlike process_time(), which measures the time of the whole process, thread_time() measures the execution time of the current thread only.
Example
The following program measures the CPU time spent by the current thread executing a simple loop. It records the start and end times and prints the difference.
import time start = time.thread_time() # Small computation for _ in range(1000000): pass end = time.thread_time() print("Thread CPU time:", end - start)
Following is the output for the above code:
Thread CPU time: 0.015625