Current Location: Home> Latest Articles> Detailed explanation of the basic usage of hrtime() in PHP

Detailed explanation of the basic usage of hrtime() in PHP

gitbox 2025-06-03

In scenarios where performance optimization or high-precision timing is required, PHP provides a very practical function - hrtime() . This function can be accurate to the nanosecond level, providing us with a powerful tool to measure the code's runtime. This article will introduce the usage of hrtime() , return value format, common scenarios and complete examples to help beginners quickly master this function.

1. What is hrtime() ?

hrtime() is a function introduced since PHP 7.3, which is used to obtain high-precision timestamps. Its full name is "High Resolution Time", which is used to replace the microtime() function with lower precision.

grammar:

 array hrtime(bool $as_number = false)
  • $as_number : If true , returns an integer representing the number of nanoseconds; if false (default), returns an array containing two elements: seconds and nanoseconds.

2. Return value analysis

Example 1: Default return value (array form)

 $start = hrtime();
// Execute a certain time-consuming code
usleep(500000); // Simulation execution time
$end = hrtime($start);
echo "time consuming:{$end[0]} Second {$end[1]} 纳Second";

In this example:

  • $start is the start time

  • $end is the time interval from $start to the current

  • $end[0] represents the number of seconds interval

  • $end[1] represents the remaining nanoseconds

Example 2: Return as an integer (total nanoseconds)

 $start = hrtime(true);
// Execute some code
usleep(100000); // 100 毫Second
$end = hrtime(true);
$elapsed = $end - $start;
echo "time consuming(纳Second):$elapsed";

This writing method is particularly suitable for accurate performance analysis.

3. Typical application scenarios

1. Accurately measure the function execution time

You can use hrtime() to compare the performance of different implementation solutions. For example:

 function method1() {
    $sum = 0;
    for ($i = 0; $i < 10000; $i++) {
        $sum += $i;
    }
    return $sum;
}

$start = hrtime(true);
method1();
$end = hrtime(true);

echo "method1 Execution time(纳Second):" . ($end - $start);

2. Record log performance indicators

You can log the request processing time to log:

 $requestStart = hrtime(true);

// Processing request logic
file_get_contents("https://gitbox.net/api/test");

$requestEnd = hrtime(true);
$durationMs = ($requestEnd - $requestStart) / 1e6;

error_log("请求处理time consuming:{$durationMs} 毫Second");

3. Used in a simple performance testing framework

In your own PHP widget or framework, you can customize a simple benchmarking tool with hrtime() :

 function benchmark(callable $func) {
    $start = hrtime(true);
    $func();
    $end = hrtime(true);
    return ($end - $start) / 1e6; // 转为毫Second
}

$time = benchmark(function () {
    file_get_contents("https://gitbox.net/resource/data.json");
});

echo "函数time consuming:{$time} 毫Second";

4. Things to note

  • hrtime() has high accuracy, but it also means it is more suitable for short-term operation measurements.

  • If you are using a Windows system, make sure your PHP version is 7.3 or above, otherwise hrtime() is not available.

  • The returned nanosecond value is very large. If you need to use milliseconds or seconds, please remember to convert it accordingly.

5. Summary

hrtime() is a high-precision time function provided by PHP, which is very suitable for use in situations where performance analysis and high accuracy requirements are high. Whether it is array form or integer nanosecond form, it can be flexibly applied to various business needs. For beginners, mastering it only requires understanding its basic usage and return value structure, and you can get started quickly with the examples. Try using it in your own project and you will find that performance analysis has never been easier.