The Wayback Machine - https://web.archive.org/web/20240817194354/https://www.geeksforgeeks.org/python-time-process_time_ns-method/
Open In App

Python | time.process_time_ns() method

Last Updated : 17 Sep, 2019
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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




# 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




# 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



Previous Article
Next Article

Similar Reads

Time difference between expected time and given time
Given the initial clock time h1:m1 and the present clock time h2:m2, denoting hour and minutes in 24-hours clock format. The present clock time h2:m2 may or may not be correct. Also given a variable K which denotes the number of hours passed. The task is to calculate the delay in seconds i.e. time difference between expected time and given time. Ex
5 min read
Pandas Series dt.time | Extract Time from Time Stamp in Series
The Series.dt.time attribute returns a NumPy array containing time values of the timestamps in a Pandas series. Example C/C++ Code import pandas as pd sr = pd.Series(['2012-10-21 09:30', '2019-7-18 12:30', '2008-02-2 10:30', '2010-4-22 09:25', '2019-11-8 02:22']) idx = ['Day 1', 'Day 2', 'Day 3', 'Day 4', 'Day 5'] sr.index = idx sr = pd.to_datetime
2 min read
Python | time.time() method
Time module in Python provides various time-related functions. This module comes under Python’s standard utility modules. time.time() method of Time module is used to get the time in seconds since epoch. The handling of leap seconds is platform dependent. Note: The epoch is the point where the time starts, and is platform dependent. On Windows and
2 min read
Time Functions in Python | Set 1 (time(), ctime(), sleep()...)
Python has defined a module, "time" which allows us to handle various operations regarding time, its conversions and representations, which find its use in various applications in life. The beginning of time started measuring from 1 January, 12:00 am, 1970 and this very time is termed as "epoch" in Python. Operations on Time in Python Python time.t
4 min read
Python program to find difference between current time and given time
Given two times h1:m1 and h2:m2 denoting hours and minutes in 24 hours clock format. The current clock time is given by h1:m1. The task is to calculate the difference between two times in minutes and print the difference between two times in h:m format. Examples: Input : h1=7, m1=20, h2=9, m2=45 Output : 2 : 25 The current time is 7 : 20 and given
2 min read
How To Highlight a Time Range in Time Series Plot in Python with Matplotlib?
A time series plot is a plot which contains data which is being measured over a period of time, for example, a gross domestic product of a country, the population of the world and many other data. Sometimes we want to highlight a specific period of the timeline so that it is easier for the observer to read specific data. We can highlight a time ran
4 min read
MoviePy – Changing Image and Time at same time of Video Clip
In this article we will see how we can change display frames and time line of the video file clip in MoviePy at a same time. MoviePy is a Python module for video editing, which can be used for basic operations on videos and GIF's. We use fl_image method to alter the display frames and fl_time method to alter the time, although we can do these task
2 min read
Real-Time Edge Detection using OpenCV in Python | Canny edge detection method
Edge detection is one of the fundamental image-processing tasks used in various Computer Vision tasks to identify the boundary or sharp changes in the pixel intensity. It plays a crucial role in object detection, image segmentation and feature extraction from the image. In Real-time edge detection, the image frame coming from a live webcam or video
5 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.monotonic() method Syntax: Syntax: time.monotonic()
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 are as follow: clock : time.clock() monotonic : tim
2 min read
Python | time.asctime() method
Python time method time.asctime() is used to convert a tuple or a time.struct_time object representing a time as returned by time.gmtime() or time.localtime() method to a string of the following form: Day Mon Date Hour:Min:Sec Year For example: Thu 08 22 10:46:56 2019Python time asctime() Syntax: time.asctime([t]) Python time asctime() Parameter: t
3 min read
Python | time.localtime() method
Time module in Python provides various time-related functions. This module comes under Python’s standard utility modules. time.localtime() method of Time module is used to convert a time expressed in seconds since the epoch to a time.struct_time object in local time. To convert the given time in seconds since the epoch to a time.struct_time object
3 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 convert the given time in seconds since the epoch
3 min read
Python | time.time_ns() method
Time module in Python provides various time-related functions. This module comes under Python’s standard utility modules. time.time_ns() method of Time module is used to get the time in nanoseconds since the epoch. To get the time in seconds since the epoch, we can use time.time() method. The epoch is the point where the time starts and is platform
2 min read
Python | time.clock_getres() method
Time module in Python provides various time-related functions. This module comes under Python’s standard utility modules. time.clock_getres() method of Time module is used to get the resolution or precision of the specified clock clk_id. Basically, clk_id is a integer value which represents the id of the clock. Following are the constants available
3 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 the monotonic clock value in fractional seconds. A
3 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_id but in fractional seconds. Syntax: time.clock_s
2 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 clk_id of the clock. Return type: This method doe
1 min read
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 of the clock. Following are the constants availab
3 min read
Python | time.clock_gettime() method
time.clock_gettime() method of Time module is used to get the time of the specified clock clk_id. Basically, clk_id is a integer value which represents the id of the clock. Following are the constants available on UNIX platforms that can be used as value of clk_id parameter: clk_id clk_id constant Meaning 0 time.CLOCK_REALTIME System-wide real-time
3 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 seconds since the epoch to a time.struct_time obj
4 min read
Python | time.clock() method
Time module in Python provides various time related functions. time.clock() method of time module in Python is used to get the current processor time as a floating point number expressed in seconds. As, Most of the functions defined in time module call corresponding C library function. time.clock() method also call C library function of the same na
3 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 converted into string representation. Example 1: L
2 min read
Python DateTime - time.replace() Method with Example
In this article, we will discuss the time.replace() method in Python. This method is used to manipulate objects of time class of module datetime. It is used to replace the time with the same value, except for those parameters given new values by whichever keyword arguments. Syntax: replace(year=self.year, month=self.month, day=self.day) Parameters:
2 min read
Python time altzone() Method
Python altzone() method is used to return the offset of the local DST timezone, in seconds west of UTC. We can also get the current time between the current time zone and UTC by using the timezone and altzone method. Syntax: time.altzone Example: C/C++ Code # import the time module import time # display altzone print(time.altzone) Output: 25200 Exa
1 min read
Class Method vs Static Method vs Instance Method in Python
Three important types of methods in Python are class methods, static methods, and instance methods. Each serves a distinct purpose and contributes to the overall flexibility and functionality of object-oriented programming in Python. In this article, we will see the difference between class method, static method, and instance method with the help o
5 min read
Python program to convert time from 12 hour to 24 hour format
Given a time in 12-hour AM/PM format, convert it to military (24-hour) time. Note : Midnight is 12:00:00 AM on a 12-hour clock and 00:00:00 on a 24-hour clock. Noon is 12:00:00 PM on 12-hour clock and 12:00:00 on 24-hour clock. Examples : Input : 11:21:30 PMOutput : 23:21:30Input : 12:12:20 AMOutput : 00:12:20How to Convert AM/PM to 24 Hour Time Wh
3 min read
time.sleep() in Python
Python time sleep() function suspends execution for the given number of seconds. Syntax of time sleep() Syntax : sleep(sec) Parameters : sec : Number of seconds for which the code is required to be stopped. Returns : VOID. Sometimes, there is a need to halt the flow of the program so that several other executions can take place or simply due to the
4 min read
Python | Distance-time GUI calculator using Tkinter
Prerequisites : Introduction to Tkinter | Using google distance matrix API Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with tkinter outputs the fastest and easiest wa
7 min read
Python Code for time Complexity plot of Heap Sort
Prerequisite : HeapSort Heap sort is a comparison based sorting technique based on Binary Heap data structure. It is similar to selection sort where we first find the maximum element and place the maximum element at the end. We repeat the same process for remaining element. We implement Heap Sort here, call it for different sized random lists, meas
3 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg