Re: Re: negative numbers

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

 



* Thus wrote Ford, Mike:
> To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm
> 
> 
> 
> > -----Original Message-----
> > From: Jason Wong
> > Sent: 27/12/04 10:16
> > 
> > On Monday 27 December 2004 12:40, Richard Lynch wrote:
> > 
> > > If you want to mimic the behaviour of abs (allowing for positive
> > numbers)
> > > and performance was an issue, that:
> > > $x = ($x < 0) ? - $x : $x;
> > >
> > > is most likely faster than abs()
> > 
> > Having nothing better to do I decided to benchmark this:
> > 
> > ternary:
> > 
> >     $doo = -20;
> >     for ($i = 1; $i < 10000000; $i++) {
> >         $dah = ($doo < 0) ? - $doo : $doo;
> >     }
> > 
> > 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.  Personally, I'd want to test it with equal numbers of
> positive and negative values, and I'd want to know the contribution of the
> loop and value-setup overhead, so I'd write it like this:

This really was my first concern about the benchmark (unfair
negative usage). In what ever case one uses to get the absolute
value from a value be sure to document things like:


         /* Get the absolute value */
         $dah = ($doo < 0) ? - $doo : $doo;

where:
         $dah = abs($doo);

is self relevant.


Curt
-- 
Quoth the Raven, "Nevermore."

-- 
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