Class runTime {
- Var $ StartTime = 0;
- Var $ StopTime = 0;
- Var $ TimeSpent = 0;
Function start (){
- $ This-> StartTime = microtime ();
- }
Function stop (){
- $ This-> StopTime = microtime ();
- }
Function spent (){
- If ($ this-> TimeSpent ){
- Return $ this-> TimeSpent;
- } Else {
- $ StartMicro = substr ($ this-> StartTime, 0, 10 );
- $ StartSecond = substr ($ this-> StartTime, 11, 10 );
- $ StopMicro = substr ($ this-> StopTime, 0, 10 );
- $ StopSecond = substr ($ this-> StopTime, 11, 10 );
- $ Start = floatval ($ StartMicro) + $ StartSecond;
- $ Stop = floatval ($ StopMicro) + $ StopSecond;
- $ This-> TimeSpent = $ stop-$ start;
- Return round ($ this-> TimeSpent, 8 );
- }
- } // End function
- }
Note: 1. Why is encapsulation inadequate? During usage, I found that the attributes of these classes do not need to appear in the var (public) form. since the class is used, I will follow some basic rules of object-oriented, these variables can be controlled using private access. 2. is microtime well used? Instructions on microtime in the manual: Definition and usageThe microtime () function returns the current Unix timestamp and the number of microseconds. If no optional parameter is required, this function returns a string in the format of "msec sec", where sec is the current number of seconds since the Unix epoch (0:00:00 January 1, 1970 GMT, msec is in microseconds. The two parts of the string are returned in seconds. In PHP5 and later versions, the parameter true is acceptable, so that the floating point can be directly returned, and the efficiency will be much higher than the current. Below is a small piece of code found on the Internet, which can be used as a reference:
Function microtime_float3 (){
- Return microtime (true );
- }
Function microtime_float2 (){
- If (PHP_VERSION> 5 ){
- Return microtime (true );
- } Else {
- List ($ usec, $ sec) = explode ("", microtime ());
- Return (float) $ usec + (float) $ sec );
- }
- }
Function microtime_float (){
- List ($ usec, $ sec) = explode ("", microtime ());
- Return (float) $ usec + (float) $ sec );
- }
Function runtime ($ t1 ){
- Return number_format (microtime_float ()-$ t1) * 1000, 4). 'Ms ';
- }
$ T1 = microtime_float ();
- For ($ I = 0; I I <10000; $ I ++ ){
- Microtime_float ();
- }
- Echo "microtime_float ==== ";
- Echo runtime ($ t1 ).'
';
- $ T1 = microtime (true );
For ($ I = 0; I I <10000; $ I ++ ){
- Microtime (true );
- }
- Echo "microtime_true ==== ";
- Echo runtime ($ t1 ).'
';
- $ T1 = microtime (true );
For ($ I = 0; I I <10000; $ I ++ ){
- Microtime_float2 ();
- }
Echo "microtime_float2 ==== ";
- Echo runtime ($ t1 ).'
';
- $ T1 = microtime (true );
For ($ I = 0; I I <10000; $ I ++ ){
- Microtime_float3 ();
- }
- Echo "microtime_float3 ==== ";
- Echo runtime ($ t1 ).'
';
- ?>
Running result of winxp on the local machine: microtime_float ==== 109.5631 ms microtime_true ==== 38.8160 ms microtime_float2 ===== 52.7902 ms microtime_float3 ==== 45.0699 ms Linux running results: microtime_float ==== 47.2510 ms microtime_true ==== 9.2051 ms microtime_float2 ===== 16.3319 ms microtime_float3 ==== 12.2800 ms |