High-Resolution Monotonic Timer

Sebastian BergmannArne BlankertsStefan Priebsch |

Available since PHP 4, the microtime() function returns the current Unix timestamp with microseconds:

1 2
var_dump ( microtime ( ) ) ;
var_dump ( microtime ( true ) ) ;

Executing the code shown above will print the output shown below:

string(21) "0.10677600 1586956167" float(1586956167.1068)

Without the true argument, microtime() returns a string in the format "microseconds seconds" where the latter is the number of seconds since the Unix epoch, and the former is the number of microseconds since "seconds".

With the true argument, microtime() returns a float value that represents the number of seconds since the Unix epoche with microsecond granularity.

The microtime() function looks at the system clock to perform its task. Depending on the use case, for instance when you want to measure performance, this may be problematic.

A common use case for microtime() is measuring the time between two points in time:

1 2 3 4 5 6 7
$start   =   microtime ( true ) ;

// ... do something ...

$end   =   microtime ( true ) ;

$elapsed   =   $end   -   $start ;

The intent of the code shown above is that $elapsed contains the time, in microseconds, that passed between the two points in time where microtime() was invoked.

If the system clock is changed between the two invocations of microtime() then the result in $elapsed will be wrong. The hrtime() function was introduced in PHP 7.3 to solve this problem.

hrtime() implements a so called high-resolution monotonic timer. The "high-resolution" part refers to the fact that the time resolution of hrtime() is nanoseconds where microtime() only has microsecond resolution. "monotonic" is a reference to the concept of "monotonic functions" in mathematics: a function is monotonic when its values are either entirely non-increasing or entirely non-decreasing.

The values returned by the hrtime() function are monotonically increasing, meaning they always increase (or stay the same) and never decrease.

1 2
var_dump ( hrtime ( ) ) ;
var_dump ( hrtime ( true ) ) ;

Executing the code shown above will print the output shown below:

array(2) { [0]=> int(30955) [1]=> int(926320146) } int(30955926404559)

hrtime() returns a timestamp at an unspecified point in the past. Without the true argument, this timestamp is represented as an array where the first element contains the number of seconds whereas the second element contains the number of nanoseconds.

With the true argument, hrtime() returns the timestamp as nanoseconds. On a 64-bit system the returned timestamp is an integer, on a 32-bit system it is a float.

hrtime() does not depend on the system clock to perform its job. Even if the system clock is changed between the two invocations of hrtime() then the result in $elapsed will still be correct:

1 2 3 4 5 6 7
$start   =   hrtime ( true ) ;

// ... do something ...

$end   =   hrtime ( true ) ;

$elapsed   =   $end   -   $start ;

Furthermore, the value of $elapsed is now more accurate which is important when taking performance measurements.


This article is an excerpt from our eBook PHP 7 Explained.