Home  >  Article  >  Backend Development  >  A php class that calculates program running time

A php class that calculates program running time

WBOY
WBOYOriginal
2016-07-25 09:03:291224browse
  1. /**
  2. * Calculate program running time
  3. * filename: js_runtime.php
  4. */
  5. class Timer {
  6. private $StartTime = 0;//Program running start time
  7. private $StopTime = 0;//Program running end time
  8. private $TimeSpent = 0;//It takes time for the program to run
  9. function start(){//The program starts to run
  10. $this->StartTime = microtime();
  11. }
  12. function stop(){//The program ends to run
  13. $this ->StopTime = microtime();
  14. }
  15. function spent(){//The time it takes for the program to run
  16. if ($this->TimeSpent) {
  17. return $this->TimeSpent;
  18. } else {
  19. list ($StartMicro, $StartSecond) = explode(" ", $this->StartTime);
  20. list($StopMicro, $StopSecond) = explode(" ", $this->StopTime);
  21. $start = doubleval( $StartMicro) + $StartSecond;
  22. $stop = doubleval($StopMicro) + $StopSecond;
  23. $this->TimeSpent = $stop - $start;
  24. return substr($this->TimeSpent,0,8). "Seconds";//Return the obtained program running time difference
  25. }
  26. }
  27. }
  28. $timer = new Timer();
  29. $timer->start();
  30. //...The program running code
  31. $ timer->stop();
  32. echo "The program running time is:".$timer->spent();
  33. ?>
Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn