PHP microtime gets the timestamp for floating point
- 2020-03-31 20:26:35
- OfStack
This function has been used to obtain:
See someone else's source code with microtime(true), check the manual, the original starting from PHP 5.0.0, microtime added this parameter.
reference
If the program must be running in an environment above PHP5, simply use microtime(true), which is at least twice as fast as using the microtime_float function. Here is the code for my simple test.
Operation result of winxp:
Microtime_float = = = = = 109.5631 ms
Microtime_true = = = = = 38.8160 ms
Microtime_float2 = = = = = 52.7902 ms
Microtime_float3 = = = = = 45.0699 ms
Running results on Linux:
Microtime_float = = = = = 47.2510 ms
Microtime_true = = = = = 9.2051 ms
Microtime_float2 = = = = = 16.3319 ms
Microtime_float3 = = = = = 12.2800 ms
In the PHP5 environment, use microtime(true) directly. By far the fastest. Both microtime_float2 and microtime_float3 can directly modify the function contents without changing the original program to achieve a slight performance improvement. Microtime_float2 can be written as compatible with older versions.
function microtime_float(){
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
See someone else's source code with microtime(true), check the manual, the original starting from PHP 5.0.0, microtime added this parameter.
reference
mixed microtime ( [bool get_as_float] )
microtime() The current Unix Time stamps and microseconds. This function is supported only gettimeofday() Available under the operating system of the system call.
If the call is made without an optional argument, the "msec sec" Format returns a string in which sec Is the Unix Era ( 0:00:00 January 1, 1970 GMT ) between now and now, msec It's the microsecond part. Both parts of the string are returned in seconds.
If given get_as_float Parameter and its value is equivalent to TRUE . microtime() Returns a floating point number.
Pay attention to : get_as_float The parameter is PHP 5.0.0 New added.
If the program must be running in an environment above PHP5, simply use microtime(true), which is at least twice as fast as using the microtime_float function. Here is the code for my simple test.
<?php
function microtime_float3(){
return microtime(true);
}
function microtime_float2(){
if( PHP_VERSION > 5){
return microtime(true);
}else{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
}
function microtime_float(){
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
function runtime($t1){
return number_format((microtime_float() - $t1)*1000, 4).'ms';
}
$t1 = microtime_float();
for($i=0;$i<10000;$i++){
microtime_float();
}
echo "microtime_float=====";
echo runtime($t1).'<br>';
$t1 = microtime(true);
for($i=0;$i<10000;$i++){
microtime(true);
}
echo "microtime_true=====";
echo runtime($t1).'<br>';
$t1 = microtime(true);
for($i=0;$i<10000;$i++){
microtime_float2();
}
echo "microtime_float2=====";
echo runtime($t1).'<br>';
$t1 = microtime(true);
for($i=0;$i<10000;$i++){
microtime_float3();
}
echo "microtime_float3=====";
echo runtime($t1).'<br>';
?>
Operation result of winxp:
Microtime_float = = = = = 109.5631 ms
Microtime_true = = = = = 38.8160 ms
Microtime_float2 = = = = = 52.7902 ms
Microtime_float3 = = = = = 45.0699 ms
Running results on Linux:
Microtime_float = = = = = 47.2510 ms
Microtime_true = = = = = 9.2051 ms
Microtime_float2 = = = = = 16.3319 ms
Microtime_float3 = = = = = 12.2800 ms
In the PHP5 environment, use microtime(true) directly. By far the fastest. Both microtime_float2 and microtime_float3 can directly modify the function contents without changing the original program to achieve a slight performance improvement. Microtime_float2 can be written as compatible with older versions.