- Trending Categories
- Data Structure
- Operating System
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- 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. 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.
The syntax of time() method is as follows.
This method returns a float value that represents the seconds since the epoch.
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 The output of the above code is as follows;
The epoch is: Thu Jan 1 00:00:00 1970 1662371608.0567493
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.
- How to return the time-zone offset in minutes for the current locale?
- How to get current time in milliseconds in Python?
- How to get current date and time in Python?
- Getting current time in Python
- How to print current date and time using Python?
- How can I apply an offset on the current time in Python?
- How to return the "time" portion of the Date as a string using the current locale's conventions?
- Python program to find difference between current time and given time
- How to get the current date and time in Java?
- How to get current Time in Java 8?
- How to insert current date/time in MySQL?
- How to get the current local date and time in Kotlin?
- How to find the Number of CPU Cores in C#?