Python | time.process_time_ns () method

process_time | Python Methods and Functions

time.process_time_ns() time unit in Python is used to get the sum of the system and user processor time of the current process in nanoseconds. This method does not include time elapsed during sleep. This method is the same as process_time-function-in-python/ rel=noopener target=_blank> time.process_time () which returns the sum of the system and user cpu times of the current process in fractions of a second.

Since the checkpoint of the monotonic clock return value is not defined, only the difference between the results of successive calls is allowed .

Syntax: time.process_time_ns ()

Parameter: No parameter is required.

Return type: This method returns an integer value which represents the sum of the system and user CPU time of the current process in nanoseconds.

Code # 1: Using the time.process_time_ns()

# Python- program to explain the time.process_time_ns () method

 
# import time module

import time

 
# assignment n = 100

n = 100  

 
# Get system sum
# and user CPU time
# current process in nanoseconds
# using the time.process_time_ns () method

start = time.process_time_ns () 

 

print ( "At the beginning of the process" )

print ( "Process Time (in nanoseconds):" , start, "" )

# Here, the process time means the total of the system
# and the user's CPU time of the current process

 

 
# Print all natural numbers
# 1 to 100

 

for i in range ( 1 , n + 1 ): 

print (i, end = ''

 

print () 

 

 

end = time.process_time_ns () 

 

print ( "At the end of the process" )

print ( " Process time (in nanoseconds): " , end) 

print ( "Elapsed time during the process (in nanoseconds):" , end - start) 

Exit :

 At the beginning of the process Process Time (in nanoseconds): 31873819 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 At the end of the process Process time (in nanoseconds): 32271699 Elapsed time during the whole process (in nanoseconds): 397880 

Code # 2: To show that time.process_time_ns () does not include time elapsed during sleep

# Python program to explain the time.process_time_ns () method

 
# time module import

import time

 

 
# Get system total
# and custom CPU time
# current process in nanoseconds
# using the time.process_time_ns () method

start = time.process_time_ns () 

 

print ( "At the beginning of fir st example " )

print ( "Process Time (in nanoseconds):" , start, "" )

# Here process time means the total of the system
# and user cpu time of the current process

 
# Print all natural numbers
# from 1 to 50

 
# assignment n = 50

n = 50

 

for i in range ( 1 , n + 1 ): 

  print (i, end = ''

 

 

print () 

 

end = time.process_time_ns ()

 

print ( "At the end of the first example" )

print ( "Process time (in nanoseconds):" , end) 

print ( "Elapsed time during the first example (in nanoseconds):" , end - start) 

 

 
# Get system total
# and user CPU time
# current process in nanoseconds
# using time.process_time_ns () method

start = time.process_time_ns () 

 

print ( "At the beginning of second example " )

print ( "Process Time (in nanoseconds):" , start, "" )

# Here process time means the total of the system
# and user cpu time of the current process

 
# Print all natural numbers
# from 1 to 50

 
# assignment n = 100

n = 50

 

for i in range  ( 1 , n + 1 ): 

  print (i, end = ''

 

 

print () 

 
# pause the current one
# process in 10 seconds

time.sleep ( 10 )

  

end = time.process_time_ns ()

print ( "At the end of the second example " )

print ( "Process time (in nanoseconds):" , end) 

print ( " Elapsed time during the second example (in nanoseconds): " , end - start) 

 

  

  
# In both examples
# we can see (in the output below)
# past tense
# more or less the same
# So, suspension for
# 10 seconds not included

Exit :

 At the beginning of first example Process Time (in nanoseconds): 26901160 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 At the end of the first example Process time (in nanoseconds): 27091390 Elapsed time during the first example (in nanoseconds): 190 230 At the beginning of second example Process Time (in nanoseconds): 27186972 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 At the end of the second example Process time (in nanoseconds): 27377123 Elapsed time during the second example (in nanoseconds ): 190151 

Link: process_time_ns rel = noopener target = _b lank> https://docs.python.org/3/library/time.html#time.process_time_ns





Python | time.process_time_ns () method: StackOverflow Questions

Answer #1

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 #2

In Python 3.3+:

from subprocess import STDOUT, check_output

output = check_output(cmd, stderr=STDOUT, timeout=seconds)

output is a byte string that contains command"s merged stdout, stderr data.

check_output raises CalledProcessError on non-zero exit status as specified in the question"s text unlike proc.communicate() method.

I"ve removed shell=True because it is often used unnecessarily. You can always add it back if cmd indeed requires it. If you add shell=True i.e., if the child process spawns its own descendants; check_output() can return much later than the timeout indicates, see Subprocess timeout failure.

The timeout feature is available on Python 2.x via the subprocess32 backport of the 3.2+ subprocess module.

Answer #3

If you want to measure CPU time, can use time.process_time() for Python 3.3 and above:

import time
start = time.process_time()
# your code here    
print(time.process_time() - start)

First call turns the timer on, and second call tells you how many seconds have elapsed.

There is also a function time.clock(), but it is deprecated since Python 3.3 and will be removed in Python 3.8.

There are better profiling tools like timeit and profile, however time.process_time() will measure the CPU time and this is what you"re are asking about.

If you want to measure wall clock time instead, use time.time().

Answer #4

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 #5

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.

Tutorials