Python | time.clock() method

Time module in Python provides various time related functions.

time.clock() method of time module in Python is used to get the current processor time as a floating point number expressed in seconds.
As, Most of the functions defined in time module call corresponding C library function. time.clock() method also call C library function of the same name to get the result. The precision of returned float value depends on the called C library function.

Note: This method is deprecated since Python version 3.3 and will be removed in Python version 3.8. The behaviour of this method is platform dependent.

Syntax: time.clock()

Parameter: No parameter is required.



Return type: This method returns a float value which represents the current processor time in seconds.

Code #1: Use of time.clock() method to get current processor time

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to explain time.clock() method
  
# importing time module
import time
  
# Get the current processor
# time in seconds
pro_time = time.clock()
  
# print the current 
# processor time
print("Current processor time (in seconds):", pro_time)

chevron_right


Output:

Current processor time (in seconds): 0.042379

Code #2: Use of time.clock() method to get current processor time

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to explain time.clock() method 
  
# importing time module 
import time 
  
  
# Function to calculate factorial 
# of the given number 
def factorial(n): 
    f = 1
    for i in range(n, 1, -1): 
        f = f *
      
    return
  
  
# Get the current processor time
# in seconds at the 
# beginning of the calculation 
# using time.clock() method 
start = time.clock() 
  
# print the processor time in seconds 
print("At the beginning of the calculation"
print("Processor time (in seconds):", start, "\n"
  
  
# Calculate factorial of all 
# numbers form 0 to 9 
i = 0
fact = [0] * 10
  
while i < 10
    fact[i] = factorial(i) 
    i = i + 1
  
# Print the calculated factorial 
for i in range(0, len(fact)): 
    print("Factorial of % d:" % i, fact[i]) 
  
# Get the processor time
# in seconds at the end 
# of the calculation 
# using time.clock() method 
end = time.clock() 
  
print("\nAt the end of the calculation"
print("Processor time (in seconds):", end) 
print("Time elapsed during the calculation:", end - start)     

chevron_right


Output:

At the beginning of the calculation
Processor time (in seconds): 0.03451 

Factorial of  0: 1
Factorial of  1: 1
Factorial of  2: 2
Factorial of  3: 6
Factorial of  4: 24
Factorial of  5: 120
Factorial of  6: 720
Factorial of  7: 5040
Factorial of  8: 40320
Factorial of  9: 362880

At the end of the calculation
Processor time (in seconds): 0.034715
Time elapsed during the calculation: 0.0002050000000000038

Reference: https://docs.python.org/3/library/time.html#time.clock

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

self motivated and passionate programmer

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.