Python | time.process_time_ns() method Last Updated : 17 Sep, 2019 Comments Improve Suggest changes Like Article Like Report 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 Python3 # 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) 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 Python3 # 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 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 Comment More infoAdvertise with us Next Article Python | time.process_time_ns() method I ihritik Follow Improve Article Tags : Python python-utility Practice Tags : python Similar Reads Python | time.clock_gettime_ns() method Time module in Python provides various time-related functions. This module comes under Pythonâs standard utility modules. time.clock_gettime_ns() method of Time module is used to get the time (in nanoseconds) of the specified clock clk_id. Basically, clk_id is a integer value which represents the id 3 min read Python | time.clock_settime() method time.clock_settime() method of Time module is used to set the time (in seconds) of the specified clock clk_id. Basically, clk_id is a integer value which represents the id of the clock. Syntax: time.clock_settime(clk_id, seconds) Parameters: clk_id: A clk_id constant or an integer value representing 1 min read Python | time.clock_settime_ns() method time.clock_settime_ns() method of Time module is used to set the time (in nanoseconds) of the specified clock clk_id. Basically, clk_id is a integer value which represents the id of the clock. This method is similar to time.clock_settime() method which is used to set time of the specified clock clk_ 2 min read Python - time.ctime() Method Python time.ctime() method converts a time in seconds since the epoch to a string in local time. This is equivalent to asctime(localtime(seconds)). Current time is returned by localtime() is used when the time tuple is not present. Syntax: time.ctime([ sec ]) Parameter: sec: number of seconds to be 2 min read Python | time.get_clock_info() method Time module in Python provides various time related functions. time.get_clock_info() method in Time module is used to get the information on the specified clock name. This method return the information as a namespace object. The name of supported clocks and the method used to read that clock value a 2 min read Python | time.gmtime() method Time module in Python provides various time-related functions. This module comes under Pythonâs standard utility modules. time.gmtime() method of Time module is used to convert a time expressed in seconds since the epoch to a time.struct_time object in UTC in which tm_isdst attribute is always 0. To 3 min read Python | time.localtime() method Time module in Python provides handy tools to work with time-related tasks. One of its most useful functions is time.localtime(), which converts time expressed in seconds since the epoch (January 1, 1970) into a local time representation. It returns a time.struct_time object, which is a tuple-like s 2 min read Python | time.mktime() method time.mktime() method of Time module is used to convert a time.struct_time object or a tuple containing 9 elements corresponding to time.struct_time object to time in seconds passed since epoch in local time. This method is the inverse function of time.localtime() which converts the time expressed in 4 min read Python | time.monotonic() method Python time.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. Python time.mo 2 min read Python | time.monotonic_ns() method Time module in Python provides various time-related functions. This module comes under Pythonâs standard utility modules. time.monotonic_ns() method of time module in Python is used to get the value of a monotonic clock in nanoseconds. This method is similar to time.monotonic() method which returns 3 min read Like