Python Execution Time Measurement: Difference and Usage of time.perf_counter() and time.process_time()

When evaluating program performance, accurately measuring “how long a process took” is crucial. Python’s standard library time module provides several functions for measuring time, but you need to select the appropriate one depending on your purpose.

This article explains the usage and decisive differences between the two most representative measurement functions: time.perf_counter() and time.process_time().


目次

1. Measuring Real Time (Elapsed Time): time.perf_counter()

time.perf_counter() is the highest precision clock designed for performance measurement.

This function measures “Wall-clock time” (Real time) while the program is running.

This means it returns the actual “time elapsed on the clock,” including not only the time spent on calculation processing but also the time waiting with time.sleep() or waiting for other processes to run.

Use this function when you want to know the total duration of a process.

Execution Example: Processing Including Calculation and Waiting

This example measures the total time of sorting a list (calculation process) and a 0.5-second wait (sleep).

import time
import random

# Start measurement
start_clock = time.perf_counter()

# 1. Process using CPU (List sorting)
data_list = [random.random() for _ in range(100000)]
data_list.sort()

# 2. Process NOT using CPU (0.5 seconds wait)
time.sleep(0.5)

# End measurement
end_clock = time.perf_counter()

# Calculate elapsed time
elapsed_time = end_clock - start_clock

print(f"perf_counter result: {elapsed_time:.5f} seconds")

Output Example:

perf_counter result: 0.53421 seconds

The total of the time taken for sorting (approx. 0.03 seconds) and the sleep time (0.5 seconds) is measured.


2. Measuring CPU Time: time.process_time()

time.process_time() measures the “CPU time” actually used by the current process.

The biggest feature of this function is that it does not include sleep time like time.sleep() or I/O waits (file reading/writing or communication wait time).

Use this when you want to know only the time the program purely occupied the CPU for calculation processing.

Execution Example: Measuring the Same Process

We measure the same “Sort processing + 0.5 seconds wait” using process_time().

import time
import random

# Start measurement
start_cpu = time.process_time()

# 1. Process using CPU
data_list = [random.random() for _ in range(100000)]
data_list.sort()

# 2. Process NOT using CPU (0.5 seconds wait)
# process_time does NOT count this wait time
time.sleep(0.5)

# End measurement
end_cpu = time.process_time()

# Calculate elapsed time
elapsed_cpu = end_cpu - start_cpu

print(f"process_time result: {elapsed_cpu:.5f} seconds")

Output Example:

process_time result: 0.03125 seconds

Since the CPU was not used during the 0.5-second sleep, it is not included in the measurement result. Only the time purely taken for list generation and sorting is output.


Summary: Guidelines for Usage

Here is a summary of the differences between the two functions.

Function NameMeasurement TargetSleep TimeUsage
time.perf_counter()Real Time (Elapsed Time)IncludedWhen you want to know the wait time seen by the user or the total duration of the process.
time.process_time()CPU TimeExcludedWhen you want to measure the pure calculation speed of an algorithm or know the CPU load.

Generally, when investigating why a process feels slow, effective approach is to first grasp the total time with perf_counter(), and then use process_time() together to isolate whether it is due to CPU usage or waiting for communication etc.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次