Copy codeThe 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 ();