time.perf_counter () function in Python

Counters | perf_counter | Python Methods and Functions

The perf_counter () function always returns a floating time value in seconds. Returns the value (in fractions of a second) of the performance counter, that is, the clock with the highest available resolution for measuring short durations. It includes the time elapsed during sleep and is system-wide. The return value breakpoint is undefined, so only the difference between the results of successive calls is allowed. In between, we can use time.sleep () and similarly fuctions.

Code # 1: Understand the use of perf_counter .

# Python program to display time from perf_counter ()

from time import perf_counter

 
# integer input from user, 2 inputs per line

n, m = map ( int , input () .split ()) 

 
# Start stopwatch / counter

t1_start = perf_counter () 

 

for i in range (n):

t = int ( input ()) # user entered n times

if t % m = = 0 :

  print (t) 

  
# Stop stopwatch / counter

t1_stop = perf_counter ()

 

print ( "Elapsed time:" , t1_stop, t1_start) 

  

  

print ( "Elapsed time during the whole program in seconds: " ,

  t1_stop - t1_start)

Exit od:

pref_counter_ns ():
It always gives an integer time value in nanoseconds. Similar to perf_counter (), but return time in nanoseconds.

Code # 2: Using perf_counter_ns and how to implement it.

# Python program to display the time
# perf_counter_ns ()

from time import perf_counter_ns

 
# integer input from user, 2 inputs per line

n, m = map ( int , input (). split ()) 

  
# Start stopwatch / counter

t1_start = perf_counter_ns ()

 

for i in range (n):

t = int ( input ()) # user entered n times

if t % m = = 0 :

  print (t)

 
# Stop stopwatch / counter

t1_stop = perf_counter_ns ()

 

print ( "Elapsed time:" , t1_stop, 'ns' , t1_start, 'ns'

  

print ( "Elapsed time during the whole program in ns after n, m inputs:" ,

t1_stop - t1_start, ' ns' )

Output:

Compare both output from the program, since perf_counter () returns in seconds and pers_counter_ns () returns in nanoseconds.

Benefits of perf_counter ():
1. perf_counter () will give you a more accurate value than time.clock () ... 
2. From Python3.8, the time.clock () function will be removed and perf_counter will be used. 
3. We can calculate both float and integer values ​​of time in seconds and nanoseconds.





time.perf_counter () function in Python: StackOverflow Questions

Answer #1

In the case of adding a lot of rows to dataframe, I am interested in performance. So I tried the four most popular methods and checked their speed.

Performance

  1. Using .append (NPE"s answer)
  2. Using .loc (fred"s answer)
  3. Using .loc with preallocating (FooBar"s answer)
  4. Using dict and create DataFrame in the end (ShikharDua"s answer)

Runtime results (in seconds):

Approach 1000 rows 5000 rows 10 000 rows
.append 0.69 3.39 6.78
.loc without prealloc 0.74 3.90 8.35
.loc with prealloc 0.24 2.58 8.70
dict 0.012 0.046 0.084

So I use addition through the dictionary for myself.


Code:

import pandas as pd
import numpy as np
import time

del df1, df2, df3, df4
numOfRows = 1000
# append
startTime = time.perf_counter()
df1 = pd.DataFrame(np.random.randint(100, size=(5,5)), columns=["A", "B", "C", "D", "E"])
for i in range( 1,numOfRows-4):
    df1 = df1.append( dict( (a,np.random.randint(100)) for a in ["A","B","C","D","E"]), ignore_index=True)
print("Elapsed time: {:6.3f} seconds for {:d} rows".format(time.perf_counter() - startTime, numOfRows))
print(df1.shape)

# .loc w/o prealloc
startTime = time.perf_counter()
df2 = pd.DataFrame(np.random.randint(100, size=(5,5)), columns=["A", "B", "C", "D", "E"])
for i in range( 1,numOfRows):
    df2.loc[i]  = np.random.randint(100, size=(1,5))[0]
print("Elapsed time: {:6.3f} seconds for {:d} rows".format(time.perf_counter() - startTime, numOfRows))
print(df2.shape)

# .loc with prealloc
df3 = pd.DataFrame(index=np.arange(0, numOfRows), columns=["A", "B", "C", "D", "E"] )
startTime = time.perf_counter()
for i in range( 1,numOfRows):
    df3.loc[i]  = np.random.randint(100, size=(1,5))[0]
print("Elapsed time: {:6.3f} seconds for {:d} rows".format(time.perf_counter() - startTime, numOfRows))
print(df3.shape)

# dict
startTime = time.perf_counter()
row_list = []
for i in range (0,5):
    row_list.append(dict( (a,np.random.randint(100)) for a in ["A","B","C","D","E"]))
for i in range( 1,numOfRows-4):
    dict1 = dict( (a,np.random.randint(100)) for a in ["A","B","C","D","E"])
    row_list.append(dict1)

df4 = pd.DataFrame(row_list, columns=["A","B","C","D","E"])
print("Elapsed time: {:6.3f} seconds for {:d} rows".format(time.perf_counter() - startTime, numOfRows))
print(df4.shape)

P.S.: I believe my realization isn"t perfect, and maybe there is some optimization that could be done.

Answer #2

For the best measure of elapsed time (since Python 3.3), use time.perf_counter().

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.

For measurements on the order of hours/days, you don"t care about sub-second resolution so use time.monotonic() instead.

Return the value (in fractional seconds) of a monotonic clock, i.e. a clock that cannot go backwards. The clock is not affected by system clock updates. The reference point of the returned value is undefined, so that only the difference between the results of consecutive calls is valid.

In many implementations, these may actually be the same thing.

Before 3.3, you"re stuck with time.clock().

On Unix, return the current processor time as a floating point number expressed in seconds. The precision, and in fact the very definition of the meaning of “processor time”, depends on that of the C function of the same name.

On Windows, this function returns wall-clock seconds elapsed since the first call to this function, as a floating point number, based on the Win32 function QueryPerformanceCounter(). The resolution is typically better than one microsecond.


Update for Python 3.7

New in Python 3.7 is PEP 564 -- Add new time functions with nanosecond resolution.

Use of these can further eliminate rounding and floating-point errors, especially if you"re measuring very short periods, or your application (or Windows machine) is long-running.

Resolution starts breaking down on perf_counter() after around 100 days. So for example after a year of uptime, the shortest interval (greater than 0) it can measure will be bigger than when it started.


Update for Python 3.8

time.clock is now gone.

Answer #3

If you just want to measure the elapsed wall-clock time between two points, you could use time.time():

import time

start = time.time()
print("hello")
end = time.time()
print(end - start)

This gives the execution time in seconds.

Another option since 3.3 might be to use perf_counter or process_time, depending on your requirements. Before 3.3 it was recommended to use time.clock (thanks Amber). However, it is currently deprecated:

On Unix, return the current processor time as a floating point number expressed in seconds. The precision, and in fact the very definition of the meaning of “processor time”, depends on that of the C function of the same name.

On Windows, this function returns wall-clock seconds elapsed since the first call to this function, as a floating point number, based on the Win32 function QueryPerformanceCounter(). The resolution is typically better than one microsecond.

Deprecated since version 3.3: The behaviour of this function depends on the platform: use perf_counter() or process_time() instead, depending on your requirements, to have a well defined behaviour.

Answer #4

Use timeit.default_timer instead of timeit.timeit. The former provides the best clock available on your platform and version of Python automatically:

from timeit import default_timer as timer

start = timer()
# ...
end = timer()
print(end - start) # Time in seconds, e.g. 5.38091952400282

timeit.default_timer is assigned to time.time() or time.clock() depending on OS. On Python 3.3+ default_timer is time.perf_counter() on all platforms. See Python - time.clock() vs. time.time() - accuracy?

See also:

Answer #5

As of 3.3, time.clock() is deprecated, and it"s suggested to use time.process_time() or time.perf_counter() instead.

Previously in 2.7, according to the time module docs:

time.clock()

On Unix, return the current processor time as a floating point number expressed in seconds. The precision, and in fact the very definition of the meaning of “processor time”, depends on that of the C function of the same name, but in any case, this is the function to use for benchmarking Python or timing algorithms.

On Windows, this function returns wall-clock seconds elapsed since the first call to this function, as a floating point number, based on the Win32 function QueryPerformanceCounter(). The resolution is typically better than one microsecond.

Additionally, there is the timeit module for benchmarking code snippets.

Answer #6

Python 3 only:

Since time.clock() is deprecated as of Python 3.3, you will want to use time.perf_counter() for system-wide timing, or time.process_time() for process-wide timing, just the way you used to use time.clock():

import time

t = time.process_time()
#do some stuff
elapsed_time = time.process_time() - t

The new function process_time will not include time elapsed during sleep.