This is a discussion on Strange data... within the alt.comp.lang.php forums, part of the PHP Programming Forums category; I implemented a timer in my PHP page to see how long it takes to run. Here's the code: $...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
I implemented a timer in my PHP page to see how long it takes to run.
Here's the code: $this->start_time = microtime(); /* All the code */ $this->end_time = microtime(); $this->calc_time = ($this->end_time - $this->start_time); print "<tr><td colspan=\"5\">Calculated in: <b>"; printf("%." . $this->time_precision . "f", $this->calc_time); print " seconds</b></td></tr>\n"; It works, but occasionally it returns a negative value, i.e. -0.890163 seconds. Why does this happen and how can I fix it? |
|
|||
|
On Fri, 04 Jul 2003 00:54:49 -0700, Zachary Antolak wrote:
> I implemented a timer in my PHP page to see how long it takes to run. > Here's the code: > > $this->start_time = microtime(); > /* All the code */ > $this->end_time = microtime(); > $this->calc_time = ($this->end_time - $this->start_time); print "<tr><td > colspan=\"5\">Calculated in: <b>"; printf("%." . $this->time_precision . > "f", $this->calc_time); print " seconds</b></td></tr>\n"; > > It works, but occasionally it returns a negative value, i.e. -0.890163 > seconds. Why does this happen and how can I fix it? It's not working reliably because microtime() returns an array, rather than a single value. One part of the array is the time in seconds, the other is the decimal places. 0.0015 Use this code to get the microtime as a usable value: function get_microtime(){ list($micro, $sec) = explode(" ",microtime()); $mtime = (float)$sec + (float)$micro; return $mtime; } Adding the two components together would result in a number 16 digits long, which is too big for a 'double' or 'float'. PHP seems to throw away the least significant digits in cases like this, and on my machine the result appears to be accurate to at least 5 decimal places (1/10,000 s). --- Posted via news://freenews.netfront.net Complaints to news@netfront.net |
|
|||
|
2trax <2trax@salterprojects.com> wrote in message news:<pan.2003.07.04.09.53.42.929205@salterproject s.com>...
> On Fri, 04 Jul 2003 00:54:49 -0700, Zachary Antolak wrote: > > > I implemented a timer in my PHP page to see how long it takes to run. > > Here's the code: > > > > $this->start_time = microtime(); > > /* All the code */ > > $this->end_time = microtime(); > > $this->calc_time = ($this->end_time - $this->start_time); print "<tr><td > > colspan=\"5\">Calculated in: <b>"; printf("%." . $this->time_precision . > > "f", $this->calc_time); print " seconds</b></td></tr>\n"; > > > > It works, but occasionally it returns a negative value, i.e. -0.890163 > > seconds. Why does this happen and how can I fix it? > > It's not working reliably because microtime() returns an array, rather > than a single value. One part of the array is the time in seconds, the > other is the decimal places. 0.0015 > > Use this code to get the microtime as a usable value: > > function get_microtime(){ > list($micro, $sec) = explode(" ",microtime()); $mtime = (float)$sec + > (float)$micro; return $mtime; > } > > Adding the two components together would result in a number 16 digits > long, which is too big for a 'double' or 'float'. PHP seems to throw away > the least significant digits in cases like this, and on my machine the > result appears to be accurate to at least 5 decimal places (1/10,000 s). > --- > Posted via news://freenews.netfront.net > Complaints to news@netfront.net Actually, the normal times returned are usually around 0.1xxxxx and the strange ones are around -0.8xxxxx. They're like a normal value, but -1. So, I made a test for it: if ($this->calc_time < 0) { $this->calc_time = $this->calc_time + 1; } It seems to work. Is this okay to use? |
![]() |
| Thread Tools | |
| Display Modes | |
|
|