| The code is as follows: |
Copy code |
<? Php // Instance: calculates the page runtime loading time // 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. // 1. User-defined functions Function fn (){ List ($ a, $ B) = explode ('', microtime (); // gets and splits the current timestamp and number of milliseconds, and assigns the value to the variable Return $ a + $ B; } // 2. Obtain the start time $ Start_time = fn (); // 5. Loading process For ($ I = 0; I I <10000000; $ I ++ ){ // Do nothing; } // 3. Obtain the end time $ End_time = fn (); // 4. Calculate the difference value Echo $ end_time-$ start_time; // 5. Format the output Echo '<br/> '; $ T = $ end_time-$ start_time; Echo round ($ t, 2 ); ?> |
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 multiple PHP scripts may exist.
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 code is as follows: |
Copy code |
Class Timer { Private $ StartTime = 0; // The start time of the program. Private $ StopTime = 0; // end time of the program running Private $ TimeSpent = 0; // time spent on running the program Function start () {// start the program $ This-> StartTime = microtime (); } Function stop () {// The end of the program running $ This-> StopTime = microtime (); } Function spent () {// time spent on running the program If ($ this-> TimeSpent ){ Return $ this-> TimeSpent; } Else { List ($ StartMicro, $ StartSecond) = explode ("", $ this-> StartTime ); List ($ StopMicro, $ StopSecond) = explode ("", $ this-> StopTime ); $ Start = doubleval ($ StartMicro) + $ StartSecond; $ Stop = doubleval ($ StopMicro) + $ StopSecond; $ This-> TimeSpent = $ stop-$ start; Return substr ($ this-> TimeSpent,). "seconds"; // return the time difference between the obtained programs. } } } $ Timer = new Timer (); $ Timer-> start (); // Code for running the program... $ Timer-> stop (); Echo "program Running time:". $ timer-> spent (); |
Now let's look at how simple the page loading time for program computing is.
| The code is as follows: |
Copy code |
<? Php Class runtime { Var $ StartTime = 0; Var $ StopTime = 0; Function get_microtime () { List ($ usec, $ sec) = explode ('', microtime ()); Return (float) $ usec (float) $ sec ); } Function start () { $ This-> StartTime = $ this-> get_microtime (); } Function stop () { $ This-> StopTime = $ this-> get_microtime (); } Function spent () { Return round ($ this-> StopTime-$ this-> StartTime) * 1000, 1 ); } }
// Start the instance $ Runtime = new runtime; $ Runtime-> start (); // Start your code $ A = 0; For ($ I = 0; I I <1000000; $ I) { $ A = $ I; } // Your code ends $ Runtime-> stop (); Echo "page Execution time:". $ runtime-> spent (). "millisecond "; ?> |