Updated March 30, 2023
Introduction to Clock() in Python
Clock() function in python is a part of the time module that encompasses several types of time representations. The time module in python covers a. CPU time; the system takes in executing a job in a multiprocessing environment where many jobs run concurrently, b. Elapsed time or running time or Clock time a program takes to complete the execution, c. The current time is represented as an absolute time interval between a base date and now, expressed in seconds.
These time modules basically refer to the C Library repository for executing the various time-related functions. However, due to its over-dependence on platforms, these modules were deprecated in higher versions of pythons.
Representations of time in Python
Below are the Various representations of time in Python:
1. CPU time time.clock()
Processer time expressed in seconds is the time spent by the CPU ever since the process is started. In a time-sharing mode, the CPU serves multiple jobs, and each job gets only a slice of time in a round-robin fashion. Thus, CPU time consumed by a particular process can be measured by taking the time shots before the process is initiated and after the process is completed and computing the difference.
Example
In the example below, CPU time is recorded before initiating a process and after completing a process, and the difference is displayed as CPU time for executing a process. Thus, the unit of measure for time is seconds, and the process is to calculate the sum of numbers from 1 up to 100000.
# Code to explain time.clock() function in python
import time
time1 = time.clock() # Store current CPU time in time1
print("Before the start of the processing ") # Display the initial CPU time
print("CPU time (in seconds):", time1) # (in Seconds)
print()
count = 0
n = 0
while n < 100000: # Process loop to compute
count = count + n # sum of numbers from 1 to 100000
n = n +1 # end of loop
print ("Sum Value 1+2+3+4..100000 ", count) #print the computation result
time2 = time.clock() # Store the current CPU time in time2
print()
print("After the end of processing")
print("CPU time ", time2) # Display the CPU time post processing
# Compute the difference and display it
print()
print("Processing time consumed for computing 1-100000 nos", time2 - time1)
Output:
2. Elapsed time or Clock time time.time()
Time.time() function returns the time difference between a base time and now, and it is expressed in seconds. The base time is also known as epoch which varies from OS to OS. For Windows and Unix system 01-01-1970 00:00 is the epoch, and it can be known by executing a command time.gmtime(0).
Elapsed time for a process can be computed by storing time.time() value before starting the process and after completing the process and computing the difference between them.
Example
This exercise is to compute elapsed time or time interval to complete a job
# Code to compute elapsed or Clock time to execute a process in python
import time
time1 = time.time() # Store time lag between epoch & now in time1
print("Starting ") # Display the time lag from epoch 01-jan-1970
print("Epoch time (in seconds):", time1) #and now (in Seconds)
print()
count = 0
n = 0
while n < 10000000: # Process loop to compute
count = count + n # sum of numbers from 1 to 10000000
n = n +1 # end of loop
print ("Sum Value 1+2+3+4..10000000 ", count) #print the computation result
time2 = time.time() #Store time lag between epoch& now in time2
print ()
print("After the end of processing")
print("Epoch time ", time2) # Display the elapsed time post processing
#Compute the difference and display it
print()
print("Elapsed time for computing 1-10000000 nos " , time2 - time1, " seconds")
Output:
time.time() is the time lag between 01-01-1970 and Greenwich mean time or UTC (universal time coordinated) now. Approximate time gap between Epoch and now is 51 years 6 months 2 days 12 hours which works out to 51×365.25x24x60x60 + 6×30.5x24x-60-x60 + 2x24x60x60 + 12x60x60 = 1625464800 seconds (approximately).
3. Epoch time (Base line) line time.gmtime(0)
Epoch time in this OS is 1970-01-01 00:00
Example
import time
print (time.gmtime(0)) # print the base time for this OS (epoch)
print ()
print ()
print (time.time(), "Seconds from the epoch") # Current elapsed time in seconds
# (time lag from epoch 01-Jan-1970 00:00)
Output:
Sl | Attribute | Value | Remarks |
1 | Tm_year | 1970 | Base Year |
2 | Tm_mon | 1 | Month |
3 | Tm_mday | 1 | Day |
4 | Tm_hour | 0 | Hour |
5 | Tm_min | 0 | Minute |
6 | Tm_sec | 0 | Second |
7 | Tm_wday | 3 | Week day (0 – 6) 0-Monday |
8 | Tm_yday | 1 | Day’s number in the year |
9 | Tm_isdat | 0 | Whether time reflects daylight saving time applies or not,
1 – Applied 0 – Not applied |
4. Current time time.localtime()
Time.localtime() returns the current GMT or UTC in the detailed format (time tuple format)
Example
import time
print ()
print (time.localtime()) # print the GMT local time in tuple format
print ()
Output:
Sl | Attribute | Value | Remarks |
1 | Tm_year | 2021 | Base Year |
2 | Tm_mon | 7 | Month |
3 | Tm_mday | 5 | Day |
4 | Tm_hour | 8 | Hour |
5 | Tm_min | 53 | Minute |
6 | Tm_sec | 6 | Second |
7 | Tm_wday | 0 | Week day (0 – 6) 0-Monday |
8 | Tm_yday | 186 | Day’s number in the year |
9 | Tm_isdat | 0 | Whether time reflects daylight saving time applies or not,
1 – Applied 0 – Not applied |
5. Current time in readable format time.asctime()
time.asctime() returns the current time in a readable fomat
Example
import time
print ()
print (time.asctime()) # print the GMT local time in readable format
print ()
Output:
6. Sleep mode time.sleep(n)
Time.sleep(n) command keeps the process in sleep or hibernation mode for n seconds.
Conclusion – clock() in Python
Python time function, by its versatility, facilitates developers to measure the CPU time and elapsed time of any process and optimize the process accordingly by altering codes.
Recommended Articles
This is a guide to clock() in Python. Here we discuss the various representations of time in Python along with its example and code implementation. You may also have a look at the following articles to learn more –