Advertisement
Guest User

Untitled

a guest
Mar 7th, 2015
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.00 KB | None | 0 0
  1. $tries = 1000000;
  2.  
  3.  
  4.  
  5. function getName()
  6. {
  7.     $names = array('a', 'b', 'c', 'd', 'e', 'f', 'g');
  8.     shuffle($names);
  9.     return $names[0];
  10. }
  11.  
  12. class Invoker
  13. {
  14.     public function call($name)
  15.     {
  16.         // for_N();
  17.         // TODO что-то типа is_callable и массив соответствий
  18.     }
  19. }
  20. $invoker = new Invoker();
  21.  
  22.  
  23. $cnt = $tries;
  24. $switchTime = microtime(true);
  25. while ($cnt--) {
  26.  
  27.     $name = getName();
  28.     switch ($name) {
  29.  
  30.         case 'a':
  31.         case 'b':
  32.         case 'c':
  33.         case 'd':
  34.         case 'e':
  35.         case 'f':
  36.             // for_all_non_default()
  37.             if ($name == 'a') {
  38.                 // for_A()
  39.             } else if ($name == 'b') {
  40.                 // for_B()
  41.             } else if ($name == 'c') {
  42.                 // for_C()
  43.             } else if ($name == 'd') {
  44.                 // for_D()
  45.             } else if ($name == 'e') {
  46.                 // for_E()
  47.             } else if ($name == 'f') {
  48.                 // for_F()
  49.             }
  50.         break;
  51.  
  52.         default:
  53.             // for_default()
  54.  
  55.     }
  56.  
  57. }
  58. $switchTime = microtime(true) - $switchTime;
  59.  
  60.  
  61. $cnt = $tries;
  62. $callTime = microtime(true);
  63. $names = array('a', 'b', 'c', 'd', 'e', 'f');
  64. while ($cnt--) {
  65.  
  66.     $name = getName();
  67.     if (isset($names[$name])) {    // <-- самый быстрый, уделывает свитчи
  68.     //if (in_array($name, $names)) { // <-- безбожно просрал свитчам
  69.  
  70.         // for_all_non_default()
  71.  
  72.         // метот call просто пустышка,
  73.         // т.е. isset выше хоть и уделывает свитчи,
  74.         // зато тут работа не выполняется
  75.         // а если выбрать in_array, то и с пустышкой он свитчам просирает
  76.         $invoker->call($name);
  77.     } else {
  78.         // for_default()
  79.     }
  80.  
  81. }
  82. $callTime = microtime(true) - $callTime;
  83.  
  84.  
  85. var_dump(
  86.     $switchTime,
  87.     $callTime
  88. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement