time.perf_counter() function in Python


In this tutorial, we are going to learn about the time.perf_counter() method.

The method time.perf_counter() returns a float value of time in seconds. Let's see an

Example

 Live Demo

# importing the time module
import time
# printing the time
print(time.perf_counter())

Output

If you run the above code, then you will get the following result.

263.3530349

We can use the time.perf_counter() method to find the execution time of a program. Let's see an example.

Example

 Live Demo

# importing the time module
import time
# program to find the prime number
def is_prime(number):
   for i in range(2, number):
      if number % i == 0:
         return False
         return True
if __name__ == '__main__':
   number = 17377
   start_time = time.perf_counter()
   is_prime(number)
   end_time = time.perf_counter()
# printing the executing time by difference
print(end_time - start_time)

Output

If you execute the above program, then you will get the following result.

0.004171799999994619

Conclusion

If you have any doubts in the tutorial, mention them in the comment section.

Updated on: 11-Jul-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements