Summary of several methods for calculating page loading time in php

Source: Internet
Author: User
You can obtain and subtract the start time and end time of the page through the commonly used microtime (). The calculation result is a period of time that the page has been running, however, this is not necessarily the time when the page is running.

You can obtain and subtract the start time and end time of the page through the commonly used microtime (). The calculation result is a period of time that the page has been running, however, this is not necessarily the time when the page is running.

The instance code is as follows:

  1. // Instance: calculates the page runtime loading time
  2. // Analysis: get a time when the page is opened, and get a time when the load is complete. the running time is the difference between the two.
  3. // 1. user-defined functions
  4. Function fn (){
  5. List ($ a, $ B) = explode ('', microtime (); // gets and splits the current timestamp and number of milliseconds, and assigns the value to the variable
  6. Return $ a + $ B;
  7. }
  8. // 2. obtain the start time
  9. $ Start_time = fn ();
  10. // 5. loading process
  11. For ($ I = 0; I I <10000000; $ I ++ ){
  12. // Do nothing;
  13. }
  14. // 3. obtain the end time
  15. $ End_time = fn ();
  16. // 4. calculate the difference value
  17. Echo $ end_time-$ start_time;
  18. // 5. format the output
  19. Echo' ';
  20. $ T = $ end_time-$ start_time;
  21. Echo round ($ t, 2 );
  22. ?>

If you use microtime () to obtain and subtract the start time and end time of the page, the calculation result is page running.

This is not necessarily the time when the page runs, because there may be multiple PHP scripts.

This page is executed together, so I think that method is not accurate

The following is an example of how to calculate the page running time in php from the internet. For more information, see.

Recently I wrote a time computing class for program running for your reference:

The instance code is as follows:

  1. Class Timer {
  2. Private $ StartTime = 0; // The start time of the program.
  3. Private $ StopTime = 0; // end time of the program running
  4. Private $ TimeSpent = 0; // time spent on running the program
  5. Function start () {// start the program
  6. $ This-> StartTime = microtime ();
  7. }
  8. Function stop () {// The end of the program running
  9. $ This-> StopTime = microtime ();
  10. }
  11. Function spent () {// time spent on running the program
  12. If ($ this-> TimeSpent ){
  13. Return $ this-> TimeSpent;
  14. } Else {
  15. List ($ StartMicro, $ StartSecond) = explode ("", $ this-> StartTime );
  16. List ($ StopMicro, $ StopSecond) = explode ("", $ this-> StopTime );
  17. $ Start = doubleval ($ StartMicro) + $ StartSecond;
  18. $ Stop = doubleval ($ StopMicro) + $ StopSecond;
  19. $ This-> TimeSpent = $ stop-$ start;
  20. Return substr ($ this-> TimeSpent,). "seconds"; // return the time difference between the obtained programs.
  21. }
  22. }
  23. }
  24. $ Timer = new Timer ();
  25. $ Timer-> start ();
  26. // Code for running the program...
  27. $ Timer-> stop ();
  28. Echo "program Running time:". $ timer-> spent ();

Now let's look at how simple the page loading time for program computing is.

The instance code is as follows:

  1. Class runtime
  2. {
  3. Var $ StartTime = 0;
  4. Var $ StopTime = 0;
  5. Function get_microtime ()
  6. {
  7. List ($ usec, $ sec) = explode ('', microtime ());
  8. Return (float) $ usec (float) $ sec );
  9. }
  10. Function start ()
  11. {
  12. $ This-> StartTime = $ this-> get_microtime ();
  13. }
  14. Function stop ()
  15. {
  16. $ This-> StopTime = $ this-> get_microtime ();
  17. }
  18. Function spent ()
  19. {
  20. Return round ($ this-> StopTime-$ this-> StartTime) * 1000, 1 );
  21. }
  22. }
  23. // Start the instance
  24. $ Runtime = new runtime;
  25. $ Runtime-> start ();
  26. // Start your code
  27. $ A = 0;
  28. For ($ I = 0; I I <1000000; $ I)
  29. {
  30. $ A = $ I;
  31. }
  32. // Your code ends
  33. $ Runtime-> stop ();
  34. Echo "page Execution time:". $ runtime-> spent (). "millisecond ";
  35. ?>

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.