472,958 Members | 2,017 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

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 2444
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
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.