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);
    }

It turns out that abs() is slightly faster - as you might have guessed, 
otherwise I wouldn't be posting this ;-)

ternary = 14.67 secs
abs()   = 14.10 secs

The moral of the story is: if speed is important to you always do your *own* 
benchmarking.

-- 
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
------------------------------------------
/*
A lot of people I know believe in positive thinking, and so do I.  
I believe everything positively stinks.
-- Lew Col
*/

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

Reply via email to