Page 1 of 1

Microtime Issues

Posted: Tue Nov 15, 2005 4:07 pm
by John Cartwright
We'll I'm making a benchmarker class but I'm having odd values on occasion from microtime. First of all I'm using php4.3

Code: Select all

function getTime() {
			return microtime();
		}
	
		function getDifference($time1, $time2) {
			$this->lastRun = $time2 - $time1;
			return $this->lastRun;
		}

		function runCode() {
			$time1 = $this->getTime();
			exec($this->code);
			$time2 = $this->getTime();
			$this->addTime($this->getDifference($time1, $time2));
		}
Now and then I will get values such as -0.943697 or -0.946591 (those two appeared in my last test)..
Any idea what could be causing this? Could it be my laptops computing abilities are not fast enough?

Re: Microtime Issues

Posted: Tue Nov 15, 2005 4:17 pm
by redmonkey
Jcart wrote:Could it be my laptops computing abilities are not fast enough?
Nope, it's your use of microtime.

My current exectime function....

Code: Select all

<?php

function exectime($start, $end, $precision = 4)
{
  list($usec, $sec) = explode(' ', $start);
  $start_time = ((float)$usec + (float)$sec);

  list($usec, $sec) = explode(' ', $end);
  $end_time = ((float)$usec + (float)$sec);

  return round(($end_time - $start_time), $precision);
}

$start_time = microtime();
// do something
$end_time   = microtime();
echo "total time: " . exectime($start_time, $end_time) . "\x0a";
?>

Posted: Tue Nov 15, 2005 4:35 pm
by John Cartwright
Ah thanks :)