utime_s = 0; $this->utime_m = 0; $this->stime_s = 0; $this->stime_m = 0; } //This starts the clock. This just records a static time usage mark. function start() { //Get the snapshot from the system. $snapshot = getrusage(); //Record the values of interest. $this->ss_utime_s = $snapshot["ru_utime.tv_sec"]; $this->ss_utime_m = $snapshot["ru_utime.tv_usec"]; $this->ss_stime_s = $snapshot["ru_stime.tv_sec"]; $this->ss_stime_m = $snapshot["ru_stime.tv_usec"]; } //This stops the clock. This calculates the used time and adds it in to //the totals. function stop() { //Get the snapshot from the system. $snapshot = getrusage(); //Calculate and add in the elapsed user time usage since the snapshot. $this->utime_s += ($snapshot["ru_utime.tv_sec"] - $this->ss_utime_s); $this->utime_m += ($snapshot["ru_utime.tv_usec"] - $this->ss_utime_m); while ($this->utime_m < 0) { $this->utime_m += 1000000; $this->utime_s--; } //Calculate and add in the elapsed system time usage since the snapshot. $this->stime_s += ($snapshot["ru_stime.tv_sec"] - $this->ss_stime_s); $this->stime_m += ($snapshot["ru_stime.tv_usec"] - $this->ss_stime_m); while ($this->stime_m < 0) { $this->stime_m += 1000000; $this->stime_s--; } } //This returns the user time microseconds elapsed of a stopped clock, with no padding. function utime_raw_us() { return($this->utime_m); } //This returns the user time microseconds elapsed of a stopped clock, padded //with leading zeros to be six digits long. function utime_formatted_us() { $rv = $this->utime_raw_us(); while(strlen($rv) < 6) $rv = "0" . $rv; return($rv); } //This returns the system time microseconds elapsed of a stopped clock, with no padding. function stime_raw_us() { return($this->stime_m); } //This returns the system time microseconds elapsed of a stopped clock, padded //with leading zeros to be six digits long. function stime_formatted_us() { $rv = $this->stime_raw_us(); while(strlen($rv) < 6) $rv = "0" . $rv; return($rv); } //This returns the total time microseconds elapsed of a stopped clock, with no padding. function ttime_raw_us() { $rv = $this->stime_m + $this->utime_m; if ($rv >= 1000000) $rv -= 1000000; return($rv); } //This returns the total time microseconds elapsed of a stopped clock, padded //with leading zeros to be six digits long. function ttime_formatted_us() { $rv = $this->ttime_raw_us(); while(strlen($rv) < 6) $rv = "0" . $rv; return($rv); } //This returns the user time seconds elapsed of a stopped clock. function utime_s() { return($this->utime_s); } //This returns the system time seconds elapsed of a stopped clock. function stime_s() { return($this->stime_s); } //This returns the total time seconds elapsed of a stopped clock. function ttime_s() { $rv = $this->stime_s + $this->utime_s; $us_total = $this->stime_m + $this->utime_m; if ($us_total >= 1000000) $rv++; return($rv); } //This returns the standard string for a footer of a web page. No HTML formatting tags //are included. function std_web_page_cpu_time_usage_footer() { return ( "This page required approximately " . $this->ttime_s() . "." . Substr($this->ttime_formatted_us(), 0, 2) . "s (" . $this->stime_s() . "." . Substr($this->stime_formatted_us(), 0, 2) . "s system, " . $this->utime_s() . "." . Substr($this->utime_formatted_us(), 0, 2) . "s user) of CPU time on the server to generate." ); } } ?>