Python os.times() Method

Python os.times() Method

  1. Syntax of os.times() Method
  2. Example 1: Use the os.times() Method to Get the Current Global Process Time
  3. Example 2: Use the Attributes of the os.times() Method
  4. Example 3: Find the User Time of a Process Using the os.times() Method

Python os.times() method is an efficient way of finding the total time of all the current processes of the OS, including all the child processes too.

Syntax of os.times() Method

os.times()

Parameters

This method has no parameters.

Return

This method returns an object with five attributes as follows:

  1. user - This attribute represents the total user time.
  2. system - This attribute represents the total system time.
  3. children_user - This attribute represents all the child processes user time.
  4. children_system - This attribute represents all the child processes system time.
  5. elapsed - This attribute represents the real-time that has elapsed since a specified point in the past.

Example 1: Use the os.times() Method to Get the Current Global Process Time

import os

times = os.times()

print(times)

Output:

posix.times_result(user=0.01, system=0.0, children_user=0.0, children_system=0.0, elapsed=17184078.53)

Note that on Windows OS, only the user and system attributes have a known value, and the rest of the attributes have a value of 0.

Example 2: Use the Attributes of the os.times() Method

import os

times = os.times()

print("The user time: ", times.user)

print("The System time: ", times.system)

print("The children user time: ", times.children_user)

print("The children system time: ", times.children_system)

print("The elapsed time: ", times.elapsed)

Output:

The user time:  0.01
The System time:  0.0
The children user time:  0.0
The children system time:  0.0
The elapsed time:  17183187.07

Note that the method os.times() attributes are stored as elements of an array. For example, in the above code, the user time can be retrieved by writing times[0] and so on.

Example 3: Find the User Time of a Process Using the os.times() Method

import os

## A recursive function to evaluate the time used to execute it

def f(y):
    if y <= 1:
        return 1
    else:
        return f(y-1) + f(y-2)

num = 35

times1 = os.times()

value = f(num)

print ("number=%d, value=%d" % (num, value))

times2 = os.times()

userTime = times2[0] - times1[0]

systemTime = times2[1] - times1[1]

print ("User Time=%s, System Time=%s" % (userTime, systemTime))

Output:

number=35, value=14930352
User Time=2.5700000000000003, System Time=0.0

Note that the attributes in the method os.times() for Unix might work differently.

Musfirah Waseem avatar Musfirah Waseem avatar

Musfirah is a student of computer science from the best university in Pakistan. She has a knack for programming and everything related. She is a tech geek who loves to help people as much as possible.

LinkedIn

Related Article - Python OS

  • Python os.set_handle_inheritable() Method
  • Python os.set_inheritable() Method
  • Python os.stat_result Class
  • Python os.renames() Method
  • Python os.get_handle_inheritable Method
  • Python os.get_inheritable Method