Python Time perf_counter Function

The perf_counter function in Python's time module returns a high-resolution timer value as a floating-point number. This function is useful for measuring short durations with high accuracy, such as performance testing or benchmarking code.

Table of Contents

  1. Introduction
  2. perf_counter Function Syntax
  3. Examples
    • Basic Usage
    • Measuring Code Execution Time
  4. Real-World Use Case
  5. Conclusion

Introduction

The perf_counter function in Python's time module provides a high-resolution timer that is useful for performance measurements. It includes time elapsed during sleep and is system-wide. This function is ideal for timing operations and benchmarking.

perf_counter Function Syntax

Here is how you use the perf_counter function:

import time
current_time = time.perf_counter()

Parameters:

  • The perf_counter function does not take any parameters.

Returns:

  • A floating-point number representing the current value of the performance counter in seconds.

Examples

Basic Usage

Here is an example of how to use perf_counter.

Example

import time

# Getting the current performance counter value
start_time = time.perf_counter()
print("Performance counter start value:", start_time)

Output:

Performance counter start value: 39511.3588058

Measuring Code Execution Time

This example shows how to measure the execution time of a piece of code using perf_counter.

Example

import time

# Starting the performance counter
start_time = time.perf_counter()

# Code block whose execution time is to be measured
for i in range(1000000):
    pass

# Stopping the performance counter
end_time = time.perf_counter()

# Calculating the elapsed time
elapsed_time = end_time - start_time
print("Elapsed time:", elapsed_time, "seconds")

Output:

Elapsed time: 0.0203306999974302 seconds

Real-World Use Case

Benchmarking Functions

In real-world applications, the perf_counter function can be used to benchmark the performance of different functions or code blocks to optimize them.

Example

import time

def function_to_benchmark():
    sum = 0
    for i in range(1000000):
        sum += i
    return sum

# Benchmarking the function
start_time = time.perf_counter()
result = function_to_benchmark()
end_time = time.perf_counter()

# Calculating the elapsed time
elapsed_time = end_time - start_time
print(f"Function result: {result}")
print(f"Elapsed time: {elapsed_time} seconds")

Output:

Function result: 499999500000
Elapsed time: 0.025279100002080668 seconds

Conclusion

The perf_counter function provides a high-resolution timer that is ideal for performance measurements and benchmarking. By using this function, you can accurately measure the execution time of your code, helping you to optimize and improve performance.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare