Re: Re: negative numbers

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tuesday 28 December 2004 01:13, Ford, Mike wrote:

> > abs():
> >
> >     $doo = -20;
> >     for ($i = 1; $i < 10000000; $i++) {
> >         $dah = abs($doo);
> >     }
>
> That's not a valid benchmark, since only on the first pass through the loop
> is $doo negative.  

I'm not sure what you mean by that, could you please elaborate. $doo is not 
reassigned inside the loop, so AFAICS it will be negative for all passes of 
the loop.

> Personally, I'd want to test it with equal numbers of 
> positive and negative values, 

Yes, that might affect the results ...

> and I'd want to know the contribution of the 
> loop and value-setup overhead, so I'd write it like this:

... but this ought to be irrelevant because for practical purposes if you need 
a loop you use a loop along with whatever overhead it brings, it's not as if 
there are alternatives to the loop. As we're only interested in the relative 
speeds of the 2 methods, and as the loop structure is the same for both then 
effectively the loop overhead is inconsequential.


[original code snipped]

Having fixed the subtle bugs(!) running this:

<?php
    list($t0, $t1) = explode(' ', microtime());
    $oh = $t1 + $t0;
    for ($i=1; $i<10000000; $i++) {
        $doo = 20 * ($i%2?-1:1);
    }
    list($t0, $t1) = explode(' ', microtime());
    $oh = $t1 + $t0 - $oh;

    list($t0, $t1) = explode(' ', microtime());
    $tern = $t1 + $t0;
    for ($i=1; $i<10000000; $i++) {
        $doo = 20 * ($i%2?-1:1);
        $dah = ($doo<0) ? -$doo : $doo;
    }
    list($t0, $t1) = explode(' ', microtime());
    $tern = $t1 + $t0 - $tern;

    list($t0, $t1) = explode(' ', microtime());
    $abs = $t1 + $t0;
    for ($i=1; $i<10000000; $i++) {
        $doo = 20 * ($i%2?-1:1);
        $dah = abs($doo);
    }
    list($t0, $t1) = explode(' ', microtime());
    $abs = $t1 + $t0 - $abs;

    echo "<p>Overhead = ", $oh, "sec<br />\n";
    echo "Ternary = ", $tern,
         " sec; less overhead = ", $tern-$oh,
         " sec<br />\n";
    echo "Abs() = ", $abs,
         " sec; less overhead = ", $abs-$oh,
         " sec\n</p>";
?>


Results in:

Overhead = 12.942795991898sec
Ternary = 22.343024015427 sec; less overhead = 9.4002280235291 sec
Abs() = 21.590991973877 sec; less overhead = 8.6481959819794 sec

> I don't have access to a php system right now, or I'd run it (just out of
> curiosity!).  Anyone who wants to grab the above and test it is welcome...

And for the even more curious amongst you, using a straight "$doo = -20" and 
"$doo = 20" results in:

$doo = -20
Overhead = 9.3479979038239sec
Ternary = 19.242904186249 sec; less overhead = 9.8949062824249 sec
Abs() = 18.279228925705 sec; less overhead = 8.9312310218811 sec

$doo = 20
Overhead = 7.9141719341278sec
Ternary = 16.776551008224 sec; less overhead = 8.8623790740967 sec
Abs() = 17.280977964401 sec; less overhead = 9.3668060302734 sec


Conclusion if you know that all your values will be positive then using the 
ternary method will be faster (but then why the heck would you need to use 
any method at all if you already knew your values were positive!), otherwise 
abs() is faster and clearer.

-- 
Jason Wong -> Gremlins Associates -> www.gremlins.biz
Open Source Software Systems Integrators
* Web Design & Hosting * Internet & Intranet Applications Development *
------------------------------------------
Search the list archives before you post
http://marc.theaimsgroup.com/?l=php-general
------------------------------------------
/*
QOTD:
 "She's about as smart as bait."
*/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux