Python | time.process_time_ns() method

Time module in Python provides various time-related functions. This module comes under Python’s standard utility modules.

time.process_time_ns() method of time module in Python is used to get the sum of the system and user CPU time of the current process in nanoseconds. This method does not include time elapsed during sleep. This method is similar to time.process_time() method which returns the sum of the system and user CPU time of the current process in fractional seconds.

As the reference point of the returned value of monotonic clock is undefined, only the difference between the results of consecutive calls is valid.



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: Use of time.process_time_ns() method

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to explain time.process_time_ns() method
  
# importing time module
import time
  
# assigning n = 100  
n = 100 
    
# Get the sum of the system 
# and user CPU time of
# the current process in nanoseconds
# using time.process_time_ns() method
start = time.process_time_ns()  
    
print("At the beginning of the process")
print("Process Time (in nanoseconds):", start, "\n")
# Here process time means sum of the system
# and user CPU time of the current process
    
  
# Print all natural numbers
# from 1 to 100
  
for i in range(1, n + 1): 
    print(i, end =' '
    
print()  
    
  
end = time.process_time_ns() 
     
print("\nAt the end of the process")
print("Process time (in nanoseconds):", end)     
print("Elapsed time during the process (in nanoseconds):", end - start)  

chevron_right


Output:

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() method does not include the time elapsed during sleep

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to explain time.process_time_ns() method
  
# importing time module
import time
  
  
# Get the sum of the system 
# and user CPU time of
# the current process in nanoseconds
# using time.process_time_ns() method
start = time.process_time_ns()  
    
print("At the beginning of first example")
print("Process Time (in nanoseconds):", start, "\n")
# Here process time means sum of the system
# and user CPU time of the current process
    
# Print all natural numbers
# from 1 to 50
  
# assigning n = 50  
n = 50
    
for i in range(1, n + 1): 
    print(i, end =' '
      
  
print()  
  
end = time.process_time_ns()
  
print("\nAt the end of the first example")
print("Process time (in nanoseconds):", end)    
print("Elapsed time during the first example (in nanoseconds):", end-start) 
  
  
# Get the sum of the system 
# and user CPU time of
# the current process in nanoseconds
# using time.process_time_ns() method
start = time.process_time_ns()  
    
print("\nAt the beginning of second example")
print("Process Time (in nanoseconds):", start, "\n")
# Here process time means sum of the system
# and user CPU time of the current process
    
# Print all natural numbers
# from 1 to 50
  
# assigning n = 100  
n = 50
    
for i in range(1, n + 1): 
    print(i, end =' '
      
  
print()  
  
# suspend the execution of the current
# process for 10 seconds
time.sleep(10)
  
end = time.process_time_ns()
print("\nAt 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 the examples 
# we can see (in the output below)
# that elapsed time 
# is more or less the same 
# so, the suspension of process for
# 10 seconds is not included

chevron_right


Output:

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): 190230

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

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



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.