PHP microtime get floating-point timestamp _php tutorial

Source: Internet
Author: User
This function has been used to obtain:
Copy CodeThe code is as follows:
function Microtime_float () {
List ($usec, $sec) = Explode ("", Microtime ());
return (float) $usec + (float) $sec);
}

See someone else's source with Microtime (true), check the following manual, originally from PHP 5.0.0, Microtime added this parameter.
Reference
Copy CodeThe code is as follows:
Mixed Microtime ([bool Get_as_float])
Microtime () The current Unix timestamp and the number of microseconds. This function is available only under operating systems that support Gettimeofday () system calls.
If called without optional arguments, this function returns a string in the format "msec sec", where the SEC is the number of seconds since the Unix era (0:00:00 January 1, 1970 GMT) and msec is the microsecond portion. The two parts of a string are returned in seconds.
If the Get_as_float parameter is given and its value is equivalent to True,microtime (), a floating-point number is returned.
Note: The Get_as_float parameter is a new addition to PHP 5.0.0.

If the program is definitely running above PHP5, then use Microtime (true) directly, which is twice times faster than using the Microtime_float function. Here is the code for my simple test program.
Copy CodeThe code is as follows:
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 ($t 1) {
Return Number_format ((Microtime_float ()-$t 1) *1000, 4). ' Ms ';
}
$t 1 = microtime_float ();
for ($i =0; $i <10000; $i + +) {
Microtime_float ();
}
echo "microtime_float=====";
Echo Runtime ($t 1). '
';
$t 1 = microtime (true);
for ($i =0; $i <10000; $i + +) {
Microtime (TRUE);
}
echo "microtime_true=====";
Echo Runtime ($t 1). '
';
$t 1 = microtime (true);
for ($i =0; $i <10000; $i + +) {
MICROTIME_FLOAT2 ();
}
echo "microtime_float2=====";
Echo Runtime ($t 1). '
';
$t 1 = microtime (true);
for ($i =0; $i <10000; $i + +) {
MICROTIME_FLOAT3 ();
}
echo "microtime_float3=====";
Echo Runtime ($t 1). '
';
?>

Native WinXP Run Results:
Microtime_float=====109.5631ms
Microtime_true=====38.8160ms
Microtime_float2=====52.7902ms
Microtime_float3=====45.0699ms
Running results on Linux:
Microtime_float=====47.2510ms
Microtime_true=====9.2051ms
Microtime_float2=====16.3319ms
Microtime_float3=====12.2800ms
In the PHP5 environment, use Microtime directly (true); it's obviously the quickest. Both MICROTIME_FLOAT2 and MICROTIME_FLOAT3 can modify the function content directly without changing the original program to achieve a slight improvement in performance. MICROTIME_FLOAT2 can be used as an older version of the notation.

http://www.bkjia.com/PHPjc/321246.html www.bkjia.com true http://www.bkjia.com/PHPjc/321246.html techarticle always use this function to obtain: The copy code code is as follows: function microtime_float () {list ($usec, $sec) = Explode ("", Microtime ()); return (float) $usec + (float) $sec); } See ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.