PHP microtime get floating-point timestamp _php tips

Source: Internet
Author: User
Tags explode
This function has always been used to obtain:
Copy Code code as follows:

function Microtime_float () {
List ($usec, $sec) = Explode ("", Microtime ());
Return ((float) $usec + (float) $sec);
}

See other people's source code with Microtime (true), check the manual, originally from the PHP 5.0.0 began, Microtime added this parameter.
Reference
Copy Code code as follows:

Mixed Microtime ([bool Get_as_float])
Microtime () The current Unix timestamp and the number of microseconds. This function is only available under an operating system that supports Gettimeofday () system calls.
If invoked without an optional argument, 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 the msec is the microsecond portion. Both 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 the PHP 5.0.0.

If the program is definitely running above PHP5, then use Microtime (true) directly, at least twice times faster than using the Microtime_float function. Here is my simple test program code.
Copy Code code as follows:

<?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 ($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). ' <br> ';
$t 1 = microtime (true);
For ($i =0 $i <10000; $i + +) {
Microtime (TRUE);
}
echo "microtime_true=====";
Echo Runtime ($t 1). ' <br> ';
$t 1 = microtime (true);
For ($i =0 $i <10000; $i + +) {
MICROTIME_FLOAT2 ();
}
echo "microtime_float2=====";
Echo Runtime ($t 1). ' <br> ';
$t 1 = microtime (true);
For ($i =0 $i <10000; $i + +) {
MICROTIME_FLOAT3 ();
}
echo "microtime_float3=====";
Echo Runtime ($t 1). ' <br> ';
?>

The results of this machine WinXP operation:
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 PHP5 environments, direct use of Microtime (true) is clearly the fastest. 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 a compatible old version of the wording.

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.