473,474 Members | 1,822 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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
4 2468
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Grzegorz Kaczor | last post by:
Hello all, I've got a VERY strange network problem with Win2k Server and .NET. I've got one central server (hub) getting raw binary data (files) from many locations. Both server and clients...
1
by: Mei | last post by:
Hi, I'm running ASP under IIS 6 with Tomcat. During the process, Tomcat will forward to an ASP page with some data and those data will be written to MS SQL and display some information in that...
1
by: davidw | last post by:
I encountered strange issues. I have code like this sqlReader = SqlHelper.ExecuteReader(connString, System.Data.CommandType.Text,sql); It calls Microsoft.ApplicationBlocks.Data to execute a...
0
by: Grzegorz Kaczor | last post by:
Hello, I've got a VERY strange network problem with Win2k Server and .NET. I've got one central server (hub) getting raw binary data (files) from many locations. Both server and clients are...
0
by: ivb | last post by:
Hi all, I am using DB2 8.1.11.1 on NT with ASP.NET 1.1 When application make connection to database (via ADO.NET), it set "Connection timeout" parameter to 30 seconds. After, when my webpage...
4
by: Gregor KovaĨ | last post by:
Hi! When I'm using IMPORT with INSERT_UPDATE I sometimes get SQL0100W No row was found for FETCH, UPDATE or DELETE; or the result of a query is an empty table. I'm not sure why this happens....
2
by: cmt | last post by:
Greetings I have a web page with an ASP script in it that is not displaying all the data from the SQL Server DB correctly in a form. I have one line in the code, that is doing some strange...
2
by: Victor Lin | last post by:
Now I am now developing a program that base on sqlite3 in python. But there is a strange problem. That is, all data I insert into sqlite database do not goes into file in disk. It is really...
5
by: ioni | last post by:
Good day, fellows! I have a strange problem – at my site there is a flash strip, that loads data dynamically. It works fine (grabs data from the remote server and presents it), however in IE7...
0
by: charmeda103 | last post by:
when i run my program it runs with no erorrs but the output screen is giving me strange results here is whats its giving me: CONFERENCE OVERALL RANK TEAM W-L % WINS MARGIN W-L ...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.