Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference between time.perf_counter() and time.process_time()?

I am using Jupyter notebook. I'm trying to measure how long will it take to count the Avogadro's number with python. I found that time.perf_counter() and time.process_time() modules will be useful for this kind of work. So I tried both of them, but the result was totally different. What makes this kind of difference? Here are my codes.

import time  a = 10 ** 5  def AvogadroCounting():     i = 0     while i <= a:         i += 1  AvogadroCounting()  t_fract = time.perf_counter()  #time to count fraction of avogadro's number in Seconds   print(t_fract, 'secs') 

and my notebook gives 693920.393636181 secs.

import time  a = 10 ** 5  def AvogadroCounting():     i = 0     while i <= a:         i += 1  AvogadroCounting()  t_fract =  time.process_time()  #time to count fraction of avogadro's number in Seconds   print(t_fract, 'secs') 

and this gives 2048.768273 secs.

like image 357
dhk Avatar asked Sep 07 '18 11:09

dhk


People also ask

What is time Perf_counter () python?

The perf_counter() function always returns the float value of time in seconds. Return the value (in fractional seconds) of a performance counter, i.e. a clock with the highest available resolution to measure a short duration. It does include time elapsed during sleep and is system-wide.

What does Time Time () do in Python?

time() The time() function returns the number of seconds passed since epoch. For Unix system, January 1, 1970, 00:00:00 at UTC is epoch (the point where time begins).

What is monotonic clock in Python?

monotonic() method is used to get the value of a monotonic clock. A monotonic clock is a clock that can not go backward. As the reference point of the returned value of the monotonic clock is undefined, only the difference between the results of consecutive calls is valid.


1 Answers

time.perf_counter() keeps going during sleep, time.process_time() does not.

time.perf_counter() → float

Return the value (in fractional seconds) of a performance counter, i.e. a clock with the highest available resolution to measure a short duration. It does include time elapsed during sleep and is system-wide. The reference point of the returned value is undefined, so that only the difference between the results of consecutive calls is valid.

time.process_time() → float

Return the value (in fractional seconds) of the sum of the system and user CPU time of the current process. It does not include time elapsed during sleep. It is process-wide by definition. The reference point of the returned value is undefined, so that only the difference between the results of consecutive calls is valid.

See the official documentation

import time  def pc():     start = time.perf_counter()     time.sleep(1)     print(time.perf_counter()-start)  def pt():     start = time.process_time()     time.sleep(1)     print(time.process_time()-start)  pc()  # 0.99872320449432 pt()  # 0.0 
like image 93
Jonas Wolff Avatar answered Oct 19 '22 16:10

Jonas Wolff