![]() |
|
Welcome to the Computer Webmaster Gaming Console Graphics Forum forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
| |||||||
| PHP PHP for some can be one of the hardest website programming codes, so do you need help on your PHP script, if it is php4, php5 or lower this is the place for you for any PHP help. |
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 | ||
| 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? | |||
|
| Advertisements |
| | #2 | ||
| 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 | |||
|
| | #3 | ||
| 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? | |||
|
| Featured Websites | ||||
|
![]() |
| Tags: data, strange |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| strange chars | mitan | HTML | 0 | 07-01-2007 1:21 PM |
| Please help - Cracking elevation data format in data file | Kevin Fishburne | Software Programming | 10 | 06-12-2007 11:31 PM |
| a strange language | swailam | Website Reviews And Website Questions | 0 | 05-27-2007 11:14 PM |
| cvs: pear /PEAR_Frontend_Web/Frontend/Web/data package.js style.css /PEAR_Frontend_Web/Frontend/Web/data/images category.jpg config.gif download.gif error.gif info.gif infoplus.gif install.gif install_fail.gif install_ok.gif install_wait.gif login.g | Pear | 0 | 05-20-2007 7:42 PM | |
| Featured Websites | ||||
|