The Wayback Machine - https://web.archive.org/web/20240709133915/https://www.geeksforgeeks.org/time-process_time-function-in-python/
Open In App

time.process_time() function in Python

Last Updated : 03 Jul, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

time.process_time()

function always returns the float value of time in seconds. Return the value (in fractional seconds) of the sum of the system and user CPU time of the current process. It does not include time elapsed during sleep. The reference point of the returned value is undefined, so that only the difference between the results of consecutive calls is valid. As

time module

provides various time-related functions. So it is necessary to import the time module otherwise it will through error because of the definition of

time.process_time()

is present in

time module

.

Example:

Understand the usage of the process_time().

Python3 1==
# Python program to show time by process_time() 
from time import process_time

# assigning n = 50 
n = 50 

# Start the stopwatch / counter 
t1_start = process_time() 
 
for i in range(n):
    print(i, end =' ')

print() 

# Stop the stopwatch / counter
t1_stop = process_time()
 
print("Elapsed time:", t1_stop, t1_start) 
 
print("Elapsed time during the whole program in seconds:",
                                         t1_stop-t1_start) 

Output:

process_time_ns():

It always gives the integer value of time in nanoseconds. Similar to process_time() but return time as nanoseconds.This is only the basic difference.

Example:

Understand the usage of the

process_time_ns()

.

Python3 1==
# Python program to show time by process_time_ns() 
from time import process_time_ns
 
n = 50 

# Start the stopwatch / counter
t1_start = process_time_ns() 
 
for i in range(n):
    print(i, end =' ') 

print() 

# Stop the stopwatch / counter
t1_stop = process_time_ns()
 
print("Elapsed time:", t1_stop, t1_start)
 
print("Elapsed time during the whole program in nanoseconds:",
                                            t1_stop-t1_start) 

Output:

Note:

process_time()

is very different from

pref_counter()

, as

perf_counter()

calculates the program time with sleep time and if any interrupt is there but

process_counter

only calculates the system and the CPU time during the process it not includes the sleep time.

Advantages of process_time() :

1. process_time() provides the system and user CPU time of the current process. 2. We can calculate float and integer both values of time in seconds and nanoseconds. 3. Used whenever there is a need to calculate the time taken by the CPU for the particular process.

time.process_time() function in Python – FAQs

What is process_time in Python?

process_time returns the CPU time used by the current process, excluding time spent on sleep or I/O operations.

Example:

import time

start_time = time.process_time()
# Code to measure
end_time = time.process_time()

print(f"CPU time used: {end_time - start_time} seconds")

What is the time() function in Python?

The time() function returns the current time in seconds since the Unix epoch (January 1, 1970).

Example:

import time

current_time = time.time()
print(f"Current time: {current_time} seconds since the Unix epoch")

How to check the time of a process in Python?

You can use time.process_time() for CPU time or time.time() for wall-clock time to measure the duration of code execution.

Example with process_time():

import time

start_time = time.process_time()
# Code to measure
end_time = time.process_time()

print(f"CPU time used: {end_time - start_time} seconds")

Example with time():

import time

start_time = time.time()
# Code to measure
end_time = time.time()

print(f"Elapsed time: {end_time - start_time} seconds")

What is time.sleep() in Python?

time.sleep() pauses the execution of the program for a specified number of seconds.

Example:

import time

print("Waiting for 3 seconds...")
time.sleep(3) # Pause execution for 3 seconds
print("3 seconds have passed")




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
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 | 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
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
Python time strptime() function
The strptime() function in Python is used to format and return a string representation of date and time. It takes in the date, time, or both as an input, and parses it according to the directives given to it. It raises ValueError if the string cannot be formatted according to the provided directives. Syntax: time.strptime(date_time_srting,directive
2 min read
Python time.thread_time() Function
Timer objects are used to represent actions that need to be scheduled to run after a certain instant of time. These objects get scheduled to run on a separate thread that carries out the action. However, the interval that a timer is initialized with might not be the actual instant when the action was actually performed by the interpreter because it
2 min read
Python time.thread_time_ns() Function
Python time.thread_time_ns() function is used to return the value of the sum of the system and user CPU time of the current thread. It is similar to time.thread_time() function. The difference is thread_time() function return the seconds of the CPU(Computer time) but thread_time_ns() function return the nano-seconds of the CPU(Computer time) Syntax
1 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg