Content · Programming · PHP

Measuring PHP Request Execution Time

Learn how to measure elapsed time using a monotonic clock in PHP with syntax, clear code examples, practical use, and common mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

When working with PHP, you may need to measure elapsed time using a monotonic clock. This guide explains the operation, shows what its arguments mean, and uses three separate examples to make the result easier to understand.

Measuring PHP Request Execution Time: This topic combines several operations. The code demonstrates the important sequence and its effect.

What Is Measuring PHP Request Execution Time in PHP?

Measure elapsed time using a monotonic clock. This topic combines several operations. The code demonstrates the important sequence and its effect.

Syntax:

See the complete PHP example below.

Parameters and return value: The signature above identifies the arguments and return type where the API has a single call. For topic guides, follow the sequence of calls in the examples. Always distinguish a returned value from an object or variable modified by reference.

Important: Small timings vary with machine load. Use representative benchmarks and avoid logging sensitive request information.

Example 1: Basic Usage

This example introduces the operation with a short and predictable input.

<?php

$startedAt = hrtime(true);
$elapsedMilliseconds = (hrtime(true) - $startedAt) / 1_000_000;
echo $elapsedMilliseconds;

Output:

0.002955

The code focuses on the main operation. This topic combines several operations. The code demonstrates the important sequence and its effect.

Example 2: Another Case

Now compare a different input, locale, date, or option to see another part of the behavior.

<?php

$start = microtime(true);
usleep(10000);
echo round((microtime(true) - $start) * 1000, 2) . " ms";

Output:

10.1 ms

What changes? Small timings vary with machine load. Use representative benchmarks and avoid logging sensitive request information.

Practical Example: Applying It in an Application

Use the following separate example as a starting point for a small application task. Check any extension or environment requirements below before running it.

<?php

function measureExecution(callable $operation): array {
    $startedAt = hrtime(true);
    $result = $operation();
    return ["result" => $result, "milliseconds" => (hrtime(true) - $startedAt) / 1_000_000];
}
$measurement = measureExecution(fn() => array_sum(range(1, 10000)));
print_r($measurement);

Output:

Array
(
    [result] => 50005000
    [milliseconds] => 0.070377
)

The practical example demonstrates how to apply this operation to multiple values, a reusable task, or a validated input. This topic combines several operations. The code demonstrates the important sequence and its effect.

Important Behavior and Edge Cases

  • Core behavior: This topic combines several operations. The code demonstrates the important sequence and its effect.
  • Inputs and results: Small timings vary with machine load. Use representative benchmarks and avoid logging sensitive request information.
  • Alternatives: hrtime() is monotonic; microtime() reflects wall-clock time.

Comparison With Related Functions

Function or ApproachWhen to Use It
Measuring PHP Request Execution TimeMeasure elapsed time using a monotonic clock.
Related PHP approachhrtime() is monotonic; microtime() reflects wall-clock time.

Common Beginner Mistakes

MistakeCorrect Approach
Using an incorrect input or optionSmall timings vary with machine load. Use representative benchmarks and avoid logging sensitive request information.
Confusing this API with a related operationhrtime() is monotonic; microtime() reflects wall-clock time.
Assuming output is identical across environmentsCheck return values and required extensions or versions. Use explicit locale, timezone or encoding settings when they affect the result.

Key Takeaways

  • Measuring PHP Request Execution Time helps you measure elapsed time using a monotonic clock.
  • This topic combines several operations. The code demonstrates the important sequence and its effect.
  • Small timings vary with machine load. Use representative benchmarks and avoid logging sensitive request information.

Use Measuring PHP Request Execution Time when you need to measure elapsed time using a monotonic clock. Start with the first example, compare the second, and adapt the practical example to your inputs. Confirm the return value and environment requirements before adding it to production code.

Share this resourceSend this page to someone who may find it useful.