Microtime Issues

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Microtime Issues

Post 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?
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Re: Microtime Issues

Post 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";
?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Ah thanks :)
Post Reply