Phpmicrotime: getting the floating point timestamp _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Phpmicrotime gets the floating point timestamp. This function has been used for obtaining: Copy the code as follows: functionmicrotime_float () {list ($ usec, $ sec) explode (, microtime (); return (float) $ usec + (float) $ sec);} you can see that you have been using this function for a long time:

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.
Reference

The 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.


If the program must run in a PHP5 or above environment, use microtime (true) directly, which is at least twice faster than using the microtime_float function. The following is my simple program code.

The 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 ($ 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 ).'
';
$ T1 = microtime (true );
For ($ I = 0; I I <10000; $ I ++ ){
Microtime (true );
}
Echo "microtime_true ==== ";
Echo runtime ($ t1 ).'
';
$ T1 = microtime (true );
For ($ I = 0; I I <10000; $ I ++ ){
Microtime_float2 ();
}
Echo "microtime_float2 ==== ";
Echo runtime ($ t1 ).'
';
$ T1 = microtime (true );
For ($ I = 0; I I <10000; $ I ++ ){
Microtime_float3 ();
}
Echo "microtime_float3 ==== ";
Echo runtime ($ t1 ).'
';
?>


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
Linux running result:
Microtime_float ==== 47.2510 ms
Microtime_true ==== 9.2051 ms
Microtime_float2 ==== 16.3319 ms
Microtime_float3 ==== 12.2800 ms
Using microtime (true) directly in a PHP5 environment is 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.

The response 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.