
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
Return Current CPU Time in Python
In this article, we retrieve the current CPU time in Python. We use 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.time() method to get the current CPU time in seconds. The time is calculated since the epoch. It returns a floating-point number expressed in seconds.
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.
Syntax
The syntax of time() method is as follows.
time.time()
This method returns a float value that represents the seconds since the epoch.
Example
In the following example we get the current cpu time using the time.time() method from the time module and we also retrieve the epoch of a particular platform(Windows).
import time obj = time.gmtime(0) epoch = time.asctime(obj) print("The epoch is:",epoch) curr_time = time.time() print(curr_time)
Output
Output The output of the above code is as follows;
The epoch is: Thu Jan 1 00:00:00 1970 1662371608.0567493
Note
Note that different systems will have different accuracy based on their internal clock setup (ticks per second). but it's generally at least under 20ms. Also note while this function normally returns non-decreasing values, it can return a lower value than a previous call if the system clock has been set back between the two calls.