Jump to content

Variable functions overhead


lemmin

Recommended Posts

I am wondering if there is any kind of extra overhead to be considered when using PHP's variable functions. If I use a variable function to replace an if statement, it would seem that I am trading a logical procedure for some memory allocation, but there has to be some overhead to look up the function. Does anyone know intensive that is? It seems to me that replacing a switch case with a variable function would be more efficient. Am I right in assuming this?

 

Thanks for any help

Link to comment
Share on other sites

Large switches can be inefficient, because they are effectively the same as if/elseif/elseif/elseif/else.  So I would expect that at a certain switch size, variable functions would become more efficient.  Only benchmarking will say for sure though.

Link to comment
Share on other sites

Thanks for the reply. I did a rough benchmark and it looks like using a variable function becomes faster after the switch gets up to six cases; however, if it is necessary to use a check such as function_exists(), it goes up to 14. Although, if you only need to check if the variable number (if that is how you are doing it) is less than a specific number, the minimum only goes up to 7.

 

In conclusion, because switch cases are evaluated linearly, using variable functions is not efficient unless a majority of the cases that will happen are after the first 7. This means, with no weighting, that you would need 15 cases for this to be useful.

 

Also, it would seem that the number of functions created in the script does not affect the overhead for variable functions or for function_exists().

 

Here is that code I used to benchmark this. To test, the function that is run is changed from test2() to test() and compared. I have found that testing two methods in the same execution result in erroneous times.

 

Let me know if there is some logic I missed in this test.

 

Thank you.

$stamp = microtime(1);
for($i=0;$i<100000;$i++)
{
   test2(49);
}
echo '<br/><br/>'.(microtime(1)-$stamp).'<br/><br/>';

function test($num)
{
   switch ($num)
   {
      case 0: fun0();break;
      case 1: fun1();break;
      case 2: fun2();break;
      case 3: fun3();break;
      case 4: fun4();break;
      case 5: fun5();break;
      case 6: fun6();break;
      case 7: fun7();break;
      case 8: fun8();break;
      case 9: fun0();break;
      case 10: fun1();break;
      case 11: fun1();break;
      case 12: fun2();break;
      case 13: fun3();break;
      case 14: fun4();break;
      case 15: fun5();break;
      case 16: fun6();break;
      case 17: fun7();break;
      case 18: fun8();break;
      case 19: fun8();break;
      case 20: fun0();break;
      case 21: fun1();break;
      case 22: fun2();break;
      case 23: fun3();break;
      case 24: fun4();break;
      case 25: fun5();break;
      case 26: fun6();break;
      case 27: fun7();break;
      case 28: fun8();break;
      case 29: fun0();break;
      case 30: fun0();break;
      case 31: fun1();break;
      case 32: fun2();break;
      case 33: fun3();break;
      case 34: fun4();break;
      case 35: fun5();break;
      case 36: fun6();break;
      case 37: fun7();break;
      case 38: fun8();break;
      case 39: fun0();break;
      case 40: fun0();break;
      case 41: fun1();break;
      case 42: fun2();break;
      case 43: fun3();break;
      case 44: fun4();break;
      case 45: fun5();break;
      case 46: fun6();break;
      case 47: fun7();break;
      case 48: fun8();break;
      case 49: fun0();break;
   }
}

function test2($num)
{
   if ($num < 100)
   {
      $fun = 'fun'.$num;
      $fun();
   }
}

function fun0(){
   echo '0';  
}
function fun1(){
   echo '1';  
}
function fun2(){
   echo '2';  
}
function fun3(){
   echo '3';  
}
function fun4(){
   echo '4';  
}
function fun5(){
   echo '5';  
}
function fun6(){
   echo '6';  
}
function fun7(){
   echo '7';  
}
function fun8(){
   echo '8';  
}
function fun9(){
   echo '9';  
}
function fun10(){
   echo '0';  
}
function fun11(){
   echo '1';  
}
function fun12(){
   echo '2';  
}
function fun13(){
   echo '3';  
}
function fun14(){
   echo '4';  
}
function fun15(){
   echo '5';  
}
function fun16(){
   echo '6';  
}
function fun17(){
   echo '7';  
}
function fun18(){
   echo '8';  
}
function fun19(){
   echo '9';
}
function fun20(){
   echo '0';  
}
function fun21(){
   echo '1';  
}
function fun22(){
   echo '2';  
}
function fun23(){
   echo '3';  
}
function fun24(){
   echo '4';  
}
function fun25(){
   echo '5';  
}
function fun26(){
   echo '6';  
}
function fun27(){
   echo '7';  
}
function fun28(){
   echo '8';  
}
function fun29(){
   echo '9';  
}
function fun30(){
   echo '0';  
}
function fun31(){
   echo '1';  
}
function fun32(){
   echo '2';  
}
function fun33(){
   echo '3';  
}
function fun34(){
   echo '4';  
}
function fun35(){
   echo '5';  
}
function fun36(){
   echo '6';  
}
function fun37(){
   echo '7';  
}
function fun38(){
   echo '8';  
}
function fun39(){
   echo '9';  
}
function fun40(){
   echo '0';  
}
function fun41(){
   echo '1';  
}
function fun42(){
   echo '2';  
}
function fun43(){
   echo '3';  
}
function fun44(){
   echo '4';  
}
function fun45(){
   echo '5';  
}
function fun46(){
   echo '6';  
}
function fun47(){
   echo '7';  
}
function fun48(){
   echo '8';  
}
function fun49(){
   echo '9';  
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.