Strange data...

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: $...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 07-04-2003
Zachary Antolak
 
Posts: n/a
Default Strange data...

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?
Reply With Quote
  #2 (permalink)  
Old 07-04-2003
2trax
 
Posts: n/a
Default Re: Strange data...

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
Reply With Quote
  #3 (permalink)  
Old 07-04-2003
Zachary Antolak
 
Posts: n/a
Default Re: Strange data...

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?
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 10:04 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0