467,134 Members | 1,049 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,134 developers. It's quick & easy.

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?
Jul 16 '05 #1
  • viewed: 2152
Share:
4 Replies
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 ne**@netfront.net
Jul 16 '05 #2
2trax <2t***@salterprojects.com> wrote in message news:<pa****************************@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 ne**@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?
Jul 16 '05 #3
Zachary Antolak wrote:
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?


NO! It is *NOT* okay to use if you want valid data!
Copied from http://www.php.net/microtime
--------
Description

string microtime ( void)

Returns the string "msec sec" where sec is the current time
measured in the number of seconds since the Unix Epoch
(0:00:00 January 1, 1970 GMT), and msec is the microseconds
part. This function is only available on operating systems
that support the gettimeofday() system call.

Both portions of the string are returned in units of seconds.
========

So, right now if I do
<?php
$a = microtime();
echo '[ ', $a, ' ]';
?>

I get

[ 0.18475200 1057352986 ]

and in exactly five seconds I'd get

[ 0.18475200 1057352991 ]
so now I do
<?php
$first = '0.18475200 1057352986';
$second = '0.18475200 1057352991';
echo '[ ', $second - $first, ' ]';
?>

to get
[ 0 ]
I guess this isn't what you want :)

Check the very first example on the PHP manual to transform the string
"0.18475200 1057352986" to the float 1057352986.18475200
Happy Coding !!
--
"Yes, I'm positive."
"Are you sure?"
"Help, somebody has stolen one of my electrons!"
Two atoms are talking:
Jul 16 '05 #4
Pedro <he****@hotpop.com> wrote in message news:<7d******************************@news.megane tnews.com>...
Zachary Antolak wrote:
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?


NO! It is *NOT* okay to use if you want valid data!
Copied from http://www.php.net/microtime
--------
Description

string microtime ( void)

Returns the string "msec sec" where sec is the current time
measured in the number of seconds since the Unix Epoch
(0:00:00 January 1, 1970 GMT), and msec is the microseconds
part. This function is only available on operating systems
that support the gettimeofday() system call.

Both portions of the string are returned in units of seconds.
========

So, right now if I do
<?php
$a = microtime();
echo '[ ', $a, ' ]';
?>

I get

[ 0.18475200 1057352986 ]

and in exactly five seconds I'd get

[ 0.18475200 1057352991 ]
so now I do
<?php
$first = '0.18475200 1057352986';
$second = '0.18475200 1057352991';
echo '[ ', $second - $first, ' ]';
?>

to get
[ 0 ]
I guess this isn't what you want :)

Check the very first example on the PHP manual to transform the string
"0.18475200 1057352986" to the float 1057352986.18475200
Happy Coding !!


Here's the function (it's in a class):

function get_microtime()
{
list($this->usec, $this->sec) = explode(" ", microtime());
return (float)$this->usec + (float)$this->sec;
}

Is this okay?
Jul 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Grzegorz Kaczor | last post: by
1 post views Thread by Mei | last post: by
reply views Thread by Grzegorz Kaczor | last post: by
reply views Thread by ivb | last post: by
4 posts views Thread by Gregor Kovač | last post: by
2 posts views Thread by Victor Lin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.