I don’t know if you still remember the stopwatch that the teacher wore during the PE test in school? When the gun rang out, we started to run, then the stopwatch started, when we ran through the finish line, the teacher would press the button to record our results, this is a typical application of timer. What we’re going to learn today is a feature extension similar to the sports quiz stopwatch. It’s the PHP HRTime extension.

The clock ticks

First we need to understand what a system clock is. When the Linux system starts up, a clock metronome is started to time in nanoseconds, and the real name of our HRTime extension is a high-precision time extension. In other words, it is an operating system based clock metronome, capable of keeping time in nanoseconds.

1 second =1000 milliseconds =1000000 subtlety =1000000000 nanoseconds, this is seconds, milliseconds, microseconds and nanoseconds, see how accurate it is. One second equals a billion nanoseconds, which gives us a very accurate count of time intervals.

The HRTime extension can be downloaded and installed directly in PECL, just like any other normal extension.

Gets system clock tick information Ticks

So let’s start by looking at how to get the Ticks of the operating system clock, which is this tick. I believe many students have touched on it when learning the operating system. Here we will see how to get it using the HRTime extension.

print_r(hrtime()); // Array // ( // [0] => 3758 // [1] => 407409171 // ) echo hrtime(true), PHP_EOL; / / 3758407428932

The hrtime() function has been integrated into the default PHP environment since PHP7. It does not require an HRTime extension to be used. This function returns an array with no arguments. The 0th entry is the number of seconds since the system was started, and the first entry is the corresponding nanosecond count. If its argument is set to true, it will directly return the actual nanosecond timestamp that spliced the seconds and nanoseconds together.

echo HRTime\PerformanceCounter::getFrequency(), PHP_EOL; // 1000000000 echo HRTime\PerformanceCounter::getTicks(), PHP_EOL; // 3758428256236 echo HRTime\PerformanceCounter::getTicksSince(1212), PHP_EOL; // 3758428257494 $a = HRTime\PerformanceCounter::getTicks(); echo HRTime\PerformanceCounter::getTicksSince($a), PHP_EOL; / / 412

The next three functions are static functions of the PerformanceCounter object in the hrTime extension. The PerformanceCounter object means a PerformanceCounter, and getFrequency() represents the timer frequency (Ticks Ticks per second), which, as you can see, returns nanosecond units, which is 1 billion. GetTicks () returns the current clock tick time, which is the same as hrTime (true), which returns the clock tick time since the system started. The getTicksSince() method returns the time interval based on the specified number of nanoseconds, similar to date_diff(), just like our time() -time () operation. This way you can get the time interval between two runs of a piece of code, in nanoseconds.

Timer function

Next is the key content of our article, that is, the realization of timer function. As mentioned above, using getTickSince() can actually monitor the interval between runs of a piece of code, but what you’ll learn next is even more powerful.

$c = new HRTime\StopWatch;

$c->start();
for ($i = 0; $i < 1024*1024; $i++);
echo 'isRunning: ', $c->isRunning(), PHP_EOL; // isRunning: 1
$c->stop();

echo 'Time NS: ', $c->getLastElapsedTime(HRTime\Unit::NANOSECOND), PHP_EOL;
echo 'Time US: ', $c->getLastElapsedTime(HRTime\Unit::MICROSECOND), PHP_EOL;
echo 'Time MS: ', $c->getLastElapsedTime(HRTime\Unit::MILLISECOND), PHP_EOL;
echo 'Time S: ', $c->getLastElapsedTime(HRTime\Unit::SECOND), PHP_EOL;
// Time NS: 6929888
// Time US: 6929.888
// Time MS: 6.929888
// Time S: 0.006929888

echo 'Ticks: ',$c->getLastElapsedTicks(), PHP_EOL;
// Ticks: 6929888

echo 'isRunning: ',$c->isRunning(), PHP_EOL;
// 

We need to instantiate a StopWatch object and call its start() method to start a timer. The English meaning of StopWatch itself is the meaning of timer, so this object is specially designed to serve the operation of timer. The isRunning() method is used to determine if the current timer isRunning, which means that it isRunning after a start() method. If it is not in the start() and stop() range, it will return false. In the test code, we run an empty loop of 1024 by 1024, and then use the stop() method to terminate the timer.

As can be seen from the code, getLastelapsedTime () is the time interval between the start() and stop() of our above code. Its parameters can be specified in seconds, milliseconds, microseconds, nanoseconds. This method itself gets the elapsed time of the last interval. GetLastElapSedTicks () gets the clock tick information of the last interval. With the word “last time”, this object can be called multiple times to segment the time. Moreover, it is possible to summarize multiple different timings to obtain the full interval information.

$I = 0; $I = 0; $i < 1024*1024; $i++); $c->start(); for ($i = 0; $i < 1024*1024; $i++); $c->stop(); echo 'Time NS: ', $c->getLastElapsedTime(HRTime\Unit::NANOSECOND), PHP_EOL; echo 'Time US: ', $c->getLastElapsedTime(HRTime\Unit::MICROSECOND), PHP_EOL; echo 'Time MS: ', $c->getLastElapsedTime(HRTime\Unit::MILLISECOND), PHP_EOL; echo 'Time S: ', $c->getLastElapsedTime(HRTime\Unit::SECOND), PHP_EOL; // Time S: 0.00715401 echo 'All Time NS: 0.00715401 ', $c->getElapsedTime(HRTime\Unit::NANOSECOND), PHP_EOL; echo 'All Time US: ', $c->getElapsedTime(HRTime\Unit::MICROSECOND), PHP_EOL; echo 'All Time MS: ', $c->getElapsedTime(HRTime\Unit::MILLISECOND), PHP_EOL; echo 'All Time S: ', $c->getElapsedTime(HRTime\Unit::SECOND), PHP_EOL; // ALL TIME NS: 14083898 // ALL TIME US: 14083.898 // ALL TIME MS: 14.083898 // ALL TIME S: 14083898 $c- $c- $c- $c- $c- $c- $c- $c- $c- $c- $c. // All Ticks: 14083898

In this code, we inserted a loop test code between the two timing tests, which is not counted in the timing data. Then, we restart () to start a new timer, and at the end, we get the total timer using getElapSedTicks () and getElapSedTicks (). You can see that 6929888 up here plus 7154010 this time is exactly 14083898. The loop in the middle that is not in the timer is not counted in the total time.

conclusion

Is it very interesting, its effect is really the same as the stopwatch used by our PE teacher, the stopwatch of the teachers can also be recorded by many times the first to the last one of all the running results, and finally there is a total time, and in the code we are completely similar to the operation. This extension is useful for fine-grained performance debugging, as well as for business development that requires such a high-precision time lag.

Test code:

https://github.com/zhangyue0503/dev-blog/blob/master/php/202010/source/3. Learn the high-precision timer HRTime extension in PHP

Reference documents:

https://www.php.net/manual/zh/book.hrtime.php

https://www.cnblogs.com/chezxiaoqiang/archive/2012/03/23/2674386.html

Search for “Hardcore Project Manager” on all media platforms