473,769 Members | 3,084 Online
Bytes | Software Development & Data Engineering Community
+ 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\">C alculated in: <b>";
printf("%." . $this->time_precisi on . "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 2493
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\">C alculated in: <b>"; printf("%." . $this->time_precisi on .
"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.n et
Jul 16 '05 #2
2trax <2t***@salterpr ojects.com> wrote in message news:<pa******* *************** ******@salterpr ojects.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\">C alculated in: <b>"; printf("%." . $this->time_precisi on .
"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.n et


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.1847 5200
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.m eganetnews.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.1847 5200
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
1541
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 are written in C# The server is quite simple: two threads, one accepts new connections and decides whether the client is authenticated to send data or not, and the other thread serves already connected clients: performs a Socket.Select and then...
1
1327
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 particular ASP page. The Servlet has no unsafe code. The strange thing is that all the data is written correctly in the MS SQL with correct user and related information but the data might become blank or send to different users in that ASP page....
1
3044
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 sql statement. It worked fine, but after I did some modifications to my dll, I got error. The strange thing is the first call to the code runs fine, but the calls after will return the following error:
0
1250
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 written in C# The server is quite simple: two threads, one accepts new connections and decides whether the client is authenticated to send data or not, and the other thread serves already connected clients: performs a Socket.Select and then gets...
0
3574
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 requests database, and query execution time exceeds 30 seconds, the following error reported: ===
4
4953
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. The problem is that I get rejected rows because of this. Best regards,
2
1392
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 things. You can see the code below. The line in question is: WTacctTotal = objRs("WTacctTotal")
2
1717
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 strange.... Why do these data just keep in memory and discarded? All things that really store in file is the table. If I create a table, it would appears in the sqlite file.
5
2433
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 and its clones I encounter a strange problem where I can hear clicking sound non-stop (like the page is being reloaded non- stop), whereas the page is not reloading.
0
1331
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 % WINS MARGIN KSU 3-1 0.75 10.5 8-1 1.#IO 1.#J CAP 3-1 0.75 11.75 7-2 1.#IO 1.#J HEID 1-3 0.25 -4.00 3-4 ...
0
9579
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10038
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9987
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9857
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8867
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7404
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5294
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3952
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 we have to send another system
3
2812
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.