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
- }
Copy CodeAnnotations: 1. Why is the encapsulation defective? In the process of use, I found that the properties of those classes, there is no need to appear as VAR (public), since the use of class, then follow the next object-oriented basic rules, these variables can be fully controlled with private access. 2. Microtime used well enough? Some notes on Microtime in the manual: Definition and Usage the Microtime () function returns the current Unix timestamp and the number of microseconds. If called without optional arguments, this function returns a string in the format "msec sec", where the SEC is the number of seconds since the Unix era (0:00:00 January 1, 1970 GMT) and msec is the microsecond portion. The two parts of a string are returned in seconds. In PHP5 or above, you can accept the parameter true so that you can return the floating-point number directly, and it will be much more efficient than it is now. Here is a small code found on the Internet, 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 ($t 1) {
- Return Number_format ((Microtime_float ()-$t 1) *1000, 4). ' Ms ';
- }
$t 1 = microtime_float ();
- for ($i =0; $i <10000; $i + +) {
- Microtime_float ();
- }
- echo "microtime_float=====";
- Echo Runtime ($t 1). '
';
- $t 1 = microtime (true);
for ($i =0; $i <10000; $i + +) {
- Microtime (TRUE);
- }
- echo "microtime_true=====";
- Echo Runtime ($t 1). '
';
- $t 1 = microtime (true);
for ($i =0; $i <10000; $i + +) {
- MICROTIME_FLOAT2 ();
- }
echo "microtime_float2=====";
- Echo Runtime ($t 1). '
';
- $t 1 = microtime (true);
for ($i =0; $i <10000; $i + +) {
- MICROTIME_FLOAT3 ();
- }
- echo "microtime_float3=====";
- Echo Runtime ($t 1). '
';
- ?>
Copy CodeNative WinXP run Result: microtime_float=====109.5631ms microtime_true=====38.8160ms microtime_float2=====52.7902ms microtime_ Float3=====45.0699ms running results on Linux: microtime_float=====47.2510ms microtime_true=====9.2051ms microtime_float2===== 16.3319ms microtime_float3=====12.2800ms |