Sobes.tech
Junior — Middle

How can you determine the actual request processing time in a system?.

sobes.tech AI

Answer from AI

To determine the actual request processing time in Python, the time module is often used. The simplest way is to record the time before starting processing and after it finishes, then calculate the difference.

Example:

import time

def process_request():
    # simulate processing
    time.sleep(2)

start_time = time.time()  # time in seconds since epoch
process_request()
end_time = time.time()

processing_time = end_time - start_time
print(f"Request processing time: {processing_time:.3f} seconds")

For more precise measurement, time.perf_counter() can be used, which provides high-resolution timing.

start = time.perf_counter()
process_request()
end = time.perf_counter()
print(f"Processing time: {end - start:.6f} seconds")