Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

hrtime

Returns a high-resolution timer reading in nanoseconds.

Syntax

hrtime(bool $as_number = false): array|int|float|false

Parameters

ParameterTypeDescription
$as_numberboolOptional. `true` returns a single number.

Return value

array|int|float|falseThe number of nanoseconds, or an array of seconds and nanoseconds.

Examples

Measuring execution time:

<?php
 
$start = hrtime(true);
 
$sum = array_sum(range(1, 100000));
 
$ms = (hrtime(true) - $start) / 1e6;
 
echo $sum, ' in ', round($ms, 2), ' ms';

Notes

The timer is unaffected by system clock changes, so it is more reliable for measurements than microtime().

See also

  • microtime — Returns the current time with microsecond precision.
  • time — Returns the current timestamp.
  • memory_get_usage — Returns the amount of memory the program uses.

Related recipes

In the tutorial

  • Date and time — Working with dates in PHP — the date function, the DateTimeImmutable class, formats, adding to dates and the difference between them.

Thanks for reading!

Donate to the site — we work hard for you.