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:
-
- // 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' ';
- $ 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 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:
- 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 instance code is as follows:
-
- 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 ";
- ?>