Collation of microtime related knowledge in php

Source: Internet
Author: User
Collation of microtime related knowledge in php

  1. Class runTime {

  2. Var $ StartTime = 0;
  3. Var $ StopTime = 0;
  4. Var $ TimeSpent = 0;

  5. Function start (){

  6. $ This-> StartTime = microtime ();
  7. }

  8. Function stop (){

  9. $ This-> StopTime = microtime ();
  10. }

  11. Function spent (){

  12. If ($ this-> TimeSpent ){
  13. Return $ this-> TimeSpent;
  14. } Else {
  15. $ StartMicro = substr ($ this-> StartTime, 0, 10 );
  16. $ StartSecond = substr ($ this-> StartTime, 11, 10 );
  17. $ StopMicro = substr ($ this-> StopTime, 0, 10 );
  18. $ StopSecond = substr ($ this-> StopTime, 11, 10 );
  19. $ Start = floatval ($ StartMicro) + $ StartSecond;
  20. $ Stop = floatval ($ StopMicro) + $ StopSecond;
  21. $ This-> TimeSpent = $ stop-$ start;
  22. Return round ($ this-> TimeSpent, 8 );
  23. }
  24. } // End function
  25. }

Note:

1. Why is encapsulation inadequate? During usage, I found that the attributes of these classes do not need to appear in the var (public) form. since the class is used, I will follow some basic rules of object-oriented, these variables can be controlled using private access.

2. is microtime well used? Instructions on microtime in the manual:

Definition and usageThe microtime () function returns the current Unix timestamp and the number of microseconds.

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.

In PHP5 and later versions, the parameter true is acceptable, so that the floating point can be directly returned, and the efficiency will be much higher than the current. Below is a small piece of code found on the Internet, which can be used as a reference:

  1. Function microtime_float3 (){

  2. Return microtime (true );
  3. }

  4. Function microtime_float2 (){

  5. If (PHP_VERSION> 5 ){
  6. Return microtime (true );
  7. } Else {
  8. List ($ usec, $ sec) = explode ("", microtime ());
  9. Return (float) $ usec + (float) $ sec );
  10. }
  11. }

  12. Function microtime_float (){

  13. List ($ usec, $ sec) = explode ("", microtime ());
  14. Return (float) $ usec + (float) $ sec );
  15. }

  16. Function runtime ($ t1 ){

  17. Return number_format (microtime_float ()-$ t1) * 1000, 4). 'Ms ';
  18. }

  19. $ T1 = microtime_float ();

  20. For ($ I = 0; I I <10000; $ I ++ ){
  21. Microtime_float ();
  22. }
  23. Echo "microtime_float ==== ";
  24. Echo runtime ($ t1 ).'
    ';
  25. $ T1 = microtime (true );

  26. For ($ I = 0; I I <10000; $ I ++ ){

  27. Microtime (true );
  28. }
  29. Echo "microtime_true ==== ";
  30. Echo runtime ($ t1 ).'
    ';
  31. $ T1 = microtime (true );

  32. For ($ I = 0; I I <10000; $ I ++ ){

  33. Microtime_float2 ();
  34. }

  35. Echo "microtime_float2 ==== ";

  36. Echo runtime ($ t1 ).'
    ';
  37. $ T1 = microtime (true );

  38. For ($ I = 0; I I <10000; $ I ++ ){

  39. Microtime_float3 ();
  40. }
  41. Echo "microtime_float3 ==== ";
  42. Echo runtime ($ t1 ).'
    ';
  43. ?>

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 results: microtime_float ==== 47.2510 ms microtime_true ==== 9.2051 ms microtime_float2 ===== 16.3319 ms microtime_float3 ==== 12.2800 ms

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.