PHP Microtime usage Drops

Source: Internet
Author: User

Although I am a novice php, but today saw a piece of code, can not help but modify a few lines.

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

Copy Code

1. Why is the encapsulation defective? In the process of use, I found that the properties of those classes, there is no need to appear as VAR (public), since the use of class, then follow the next object-oriented basic rules, these variables can be fully controlled with private access.

2. Microtime used well enough? Some notes on Microtime in the manual:

the definition and usage of the microtime () function returns the current Unix timestamp and the number of microseconds.

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.

In PHP5 or above, you can accept the parameter true so that you can return the floating-point number directly, and it will be much more efficient than it is now.

Here is a small code found on the Internet, 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 ($t 1) {

  17. Return Number_format ((Microtime_float ()-$t 1) *1000, 4). ' Ms ';
  18. }

  19. $t 1 = microtime_float ();

  20. for ($i =0; $i <10000; $i + +) {
  21. Microtime_float ();
  22. }
  23. echo "microtime_float=====";
  24. Echo Runtime ($t 1). '
    ';
  25. $t 1 = microtime (true);

  26. for ($i =0; $i <10000; $i + +) {

  27. Microtime (TRUE);
  28. }
  29. echo "microtime_true=====";
  30. Echo Runtime ($t 1). '
    ';
  31. $t 1 = microtime (true);

  32. for ($i =0; $i <10000; $i + +) {

  33. MICROTIME_FLOAT2 ();
  34. }

  35. echo "microtime_float2=====";

  36. Echo Runtime ($t 1). '
    ';
  37. $t 1 = microtime (true);

  38. for ($i =0; $i <10000; $i + +) {

  39. MICROTIME_FLOAT3 ();
  40. }
  41. echo "microtime_float3=====";
  42. Echo Runtime ($t 1). '
    ';
  43. ?>

Copy Code

Native WinXP run Result: 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

  • Related Article

    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.