Php obtains the execution time of the target function.

Source: Internet
Author: User
Php obtains the execution time of the target function.

  1. /**
  2. * Class EfficiencyTester
  3. * Efficiency tester: test the function running time
  4. * @ Version 1.0 2013.04.13
  5. * @ Author Kross
  6. */
  7. Class EfficiencyTester {
  8. /**
  9. * Var $ testTimes
  10. * Number of Tests
  11. */
  12. Private $ testTimes = 1000;
  13. /**
  14. * Function getTime ()
  15. * Obtain the timestamp based on the time mode.
  16. * @ Param $ timeModel time mode. default value: microsecond
  17. * @ Return int timestamp
  18. */
  19. Private function getTime ($ timeModel = 'Ms '){
  20. If ($ timeModel = 'Ms '){
  21. Return microtime ();
  22. } Else if ($ timeModel ='s '){
  23. Return time ();
  24. } Else {
  25. Return microtime ();
  26. }
  27. }
  28. /**
  29. * Function testOnce ()
  30. * Test the target function once and return the running time
  31. * @ Param $ functionName target function name
  32. * @ Param $ timeModel time mode. default value: microsecond
  33. * @ Return double the time when the target function runs once (very random)
  34. */
  35. Public function testOnce ($ functionName, $ timeModel = 'Ms '){
  36. $ StartMicroTime = $ this-> getTime ($ timeModel );
  37. $ FunctionName ();
  38. $ EndMicroTime = $ this-> getTime ($ timeModel );
  39. $ CostMicroTime = $ endMicroTime-$ startMicroTime;
  40. Return $ costMicroTime;
  41. }
  42. /**
  43. * Function test ()
  44. * If the target function is tested multiple times, the running time (average value) is returned)
  45. * @ Param $ functionName target function name
  46. * @ Param $ timeModel time mode. default value: microsecond
  47. * @ Return double the running time of the target function
  48. */
  49. Public function test ($ functionName, $ timeModel = 'Ms '){
  50. $ TotalMicroTimes = 0;
  51. For ($ I = 1; $ I <= $ this-> testTimes; $ I ++ ){
  52. $ TotalMicroTimes + = $ this-> testOnce ($ functionName );
  53. }
  54. Return $ totalMicroTimes/$ this-> testTimes;
  55. }
  56. }
  57. ?>

Test code:

  1. Require_once ('../class/EfficiencyTester. class. php ');
  2. $ E = new EfficiencyTester ();
  3. Echo $ e-> test ('Rand ');
  4. ?>

In the first place, we directly used microtime () to get the time. later, we thought that if we wanted to get the running time in seconds, the write would not be polymorphism enough, and then we wrote a getTime () to obtain timestamps of different units. It seems that the running time of the target function is longer, probably because the judgment in the getTime () function takes some time.

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.