Re: Re: negative numbers

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

 



On Mon, 27 Dec 2004 18:16:21 +0800, Jason Wong <php-general@xxxxxxxxxxxx> wrote:
> 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


With PHP 5.03 I found the exact opposite results, ternary is slightly faster:

> time ./ternary.php ;time ./abs.php 

real    0m53.242s
user    0m26.655s
sys     0m0.017s

real    0m53.625s
user    0m26.732s
sys     0m0.019s


There has to be more overhead in calling a function than not,
especially after seeing the function.


PHP_FUNCTION(abs)
{
    zval **value;

    if (ZEND_NUM_ARGS()!=1||zend_get_parameters_ex(1, &value)==FAILURE) {
        WRONG_PARAM_COUNT;
    }

    convert_scalar_to_number_ex(value);

    if (Z_TYPE_PP(value) == IS_DOUBLE) {
        RETURN_DOUBLE(fabs(Z_DVAL_PP(value)));
    } else if (Z_TYPE_PP(value) == IS_LONG) {
        if (Z_LVAL_PP(value) == LONG_MIN) {
            RETURN_DOUBLE(-(double)LONG_MIN);
        } else {
            RETURN_LONG(Z_LVAL_PP(value) < 0 ? -Z_LVAL_PP(value) :
Z_LVAL_PP(value));
        }
    }

    RETURN_FALSE;
}


-- 
Greg Donald
Zend Certified Engineer
http://gdconsultants.com/
http://destiney.com/

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