PHP microtime gets the floating point Timestamp

Source: Internet
Author: User

This function has been used for obtaining: CopyCode The Code is as follows: function microtime_float (){
List ($ USEC, $ Sec) = explode ("", microtime ());
Return (float) $ USEC + (float) $ Sec );
}

We can see that microtime (true) is used in other people's source code. We checked the manual and added this parameter from PhP 5.0.0.
ReferenceCopy codeThe Code is as follows: Mixed microtime ([bool get_as_float])
Microtime () Current UNIX Timestamp and number of microseconds. This function is only available in the operating system that supports the gettimeofday () System Call.
If no optional parameter is required, this function returns a string in the format of "msec sec", where sec is the current number of seconds since the Unix epoch (0:00:00 January 1, 1970 GMT, MSEC is in microseconds. The two parts of the string are returned in seconds.
If the get_as_float parameter is given and its value is equivalent to true, microtime () returns a floating point number.
Note: The get_as_float parameter is newly added to PhP 5.0.0.

IfProgram It must be run in a PhP5 or above environment, so use microtime (true) directly, at least twice faster than using the microtime_float function. The following is my simple program code. Copy code The Code is 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 ($ T1 ){
Return number_format (microtime_float ()-$ T1) * 1000, 4). 'ms ';
}
$ T1 = microtime_float ();
For ($ I = 0; I I <10000; $ I ++ ){
Microtime_float ();
}
Echo "microtime_float ==== ";
Echo Runtime ($ T1). '<br> ';
$ T1 = microtime (true );
For ($ I = 0; I I <10000; $ I ++ ){
Microtime (true );
}
Echo "microtime_true ==== ";
Echo Runtime ($ T1). '<br> ';
$ T1 = microtime (true );
For ($ I = 0; I I <10000; $ I ++ ){
Microtime_float2 ();
}
Echo "microtime_float2 ==== ";
Echo Runtime ($ T1). '<br> ';
$ T1 = microtime (true );
For ($ I = 0; I I <10000; $ I ++ ){
Microtime_float3 ();
}
Echo "microtime_float3 ==== ";
Echo Runtime ($ T1). '<br> ';
?>

running result of WINXP on the local machine:
microtime_float ===== 109.5631 Ms
microtime_true ===== 38.8160 Ms
microtime_float2 ===== 52.7902 Ms
microtime_float3 === = 45.0699 Ms
running result 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; obviously the fastest. Both microtime_float2 and microtime_float3 can directly modify the function content without changing the original program to slightly improve the performance. Microtime_float2 can be used as a method compatible with earlier versions.

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.