PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Fri Jun 29, 2007 9:55 pm
I'm playing with a benchmarking class I'm writing and having some issues with the value returned by microtime(true)
The problem is that when I subtract a value returned by microtime, from the previous value returned by microtime, sometimes the latter return value is smaller than the first. This is what I am assuming anyway.
See the following test.
Code: Select all
<?php
class microtime_test
{
public function __construct()
{
$this->start_time = microtime(true);
}
public function test($pause = 0)
{
sleep($pause);
$new_time = microtime(true);
echo "Difference: " . ($new_time - $this->start_time) . "<br />";
$this->start_time = $new_time;
}
}
$mTest = new microtime_test();
for ($i = 0; $i < 5; $i++)
{
$mTest->test();
}
for ($i = 0; $i < 5; $i++)
{
$mTest->test(1);
}
Difference: 4.1007995605469E-5
Difference: 3.4093856811523E-5
Difference: 1.1920928955078E-5
Difference: 1.0013580322266E-5
Difference: 9.0599060058594E-6
Difference: 1.0000469684601
Difference: 1.0001080036163
Difference: 1.0001060962677
Difference: 1.0001170635223
Difference: 1.0001058578491
I can't test to see which one is larger, because they are always positive. They are also always of the same type.
What's up with this and how do I fix it?
Last edited by
Benjamin on Fri Jun 29, 2007 10:37 pm, edited 1 time in total.
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Fri Jun 29, 2007 10:03 pm
astions wrote:
Difference: 4.1007995605469E-5
Difference: 3.4093856811523E-5
Difference: 1.1920928955078E-5
Difference: 1.0013580322266E-5
Difference: 9.0599060058594E-6
Difference: 1.0000469684601
Difference: 1.0001080036163
Difference: 1.0001060962677
Difference: 1.0001170635223
Difference: 1.0001058578491
Maybe I'm missing something but aren't those all positive values?
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Fri Jun 29, 2007 10:12 pm
Yes, but it did not take more than 18 seconds to process the first 5 results. Am I supposed to convert that back into microseconds or something?
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Fri Jun 29, 2007 10:15 pm
Difference: 4.1007995605469E-5
means 4.1007995605469 * pow(10,-5) = 4.1007995605469 / 100000.
Code: Select all
<?php
$f = 4.1007995605469;
for($i=0; $i<10; $i++) {
echo $f / pow(10, $i), "\n";
}4.10079956055
0.410079956055
0.0410079956055
0.00410079956055
0.000410079956055
4.10079956055E-5
4.10079956055E-6
4.10079956055E-7
4.10079956055E-8
4.10079956055E-9
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Fri Jun 29, 2007 10:27 pm
Thank you for the help.
Besides running the equation on all numbers with an E in them. Is there a PHP function that will calculate this out?
I'm trying to keep the benchmark class as light as possible. This is not good.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Fri Jun 29, 2007 10:31 pm
You don't need to run any equation on them, they are already floating point values, native to PHP.
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Fri Jun 29, 2007 10:33 pm
I want to log these results in a human readable format in a database and in log files, so I'd like to at least round them to the nearest ten-thousandth of a second or so.
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Fri Jun 29, 2007 10:35 pm
Something like sprintf() or number_format() might help you.
(#10850)
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Fri Jun 29, 2007 10:37 pm
arborint wrote: Something like sprintf() or number_format() might help you.
Ahh number_format() did the trick.
I was sitting there going through all the math functions.
Thank you very much.
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Fri Jun 29, 2007 10:42 pm
It's still a number. The x.yzE-6 thing is just a representation.
Code: Select all
<?php
$f = 4.1007995605469 / 1000000;
echo $f, "\n";
echo $f*2, "\n";
echo $f*10, "\n";
echo $f*2000, "\n";4.10079956055E-6
8.20159912109E-6
4.10079956055E-5
0.00820159912109
But as a sidenote:
Difference: 9.0599060058594E-6
that are 9 Microseconds (micro: 10^-6)
Back in the days when I was more involved in win32 programming a reliable measurement of a timespan shorter than some milliseconds (milli: 10^-3) required quite some efford.
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Fri Jun 29, 2007 10:45 pm
volka wrote: Back in the days when I was more involved in win32 programming a reliable measurement of a timespan shorter than some milliseconds (milli: 10^-3) required quite some efford.
Yeah I bet it did. Thank you for educating me. I never paid attention in math class. They never used real world examples so I was too bored.