On 8/9/07, Wade Preston Shearer <[EMAIL PROTECTED]> wrote:
>
> If one had a long page of functions (~50) that a script selected only
> one from, which would be faster to use: a switch, an if/else
> combination, or a variable function call?


Why is everyone ignoring the variable function call?  Maybe I'm not
understanding the question.  If you take the three examples below, the third
is by far the fastest.  What am I missing?

<?php

// First example - if/else

$funcName = 'FuncOne';

if ($funcName == 'FuncOne')
    FuncOne();
else if ($funcName == 'FuncTwo')
    FuncTwo();
else if ($funcName == 'FuncThree')
    FuncThree();
else if ($funcName == 'FuncFour')
    FuncFour();

// Second example - switch

$funcName = 'FuncTwo';

switch ($funcName) {
    case 'FuncOne':
        FuncOne();
        break;
    case 'FuncTwo':
        FuncTwo();
        break;
    case 'FuncThree':
        FuncThree();
        break;
    case 'FuncFour':
        FuncFour();
        break;
}

// Third example - variable function call

$funcName = 'FuncThree';

$funcName();

?>

In the first two examples a comparison (if/else or switch) is done before
the function is called.  That (IMHO) makes them slower than the third.  I
dunno.  Maybe I've been sniffing too much screen white out but it seems
pretty clear to me.

P.S.  Actually I'm not sure how fast PHP is with the above examples.  In
C/C++ you would use function pointers to do the third example and it is by
far much faster.  Maybe PHP has more overhead to deal with.  Whatever.

-- 
Scott Hill

"May you solve interesting problems" - Author Unknown
"A fanatic is one who can't change his mind and won't change the subject." -
Sir Winston Churchill

_______________________________________________

UPHPU mailing list
[email protected]
http://uphpu.org/mailman/listinfo/uphpu
IRC: #uphpu on irc.freenode.net

Reply via email to