Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2012
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.94 KB | None | 0 0
  1. <?
  2. $input = '10 20 100 + - 20 * DUP 10 20 + * =';
  3.  
  4. $t = microtime(1);
  5.  
  6. for ($i = 0; $i < 10000; $i++) {
  7.     $stack = array();
  8.  
  9.     foreach (explode(' ', $input) as $chunk) {
  10.         switch ($chunk) {
  11.             case '+':
  12.                 array_push($stack, array_pop($stack) + array_pop($stack));
  13.                 break;
  14.             case '-':
  15.                 array_push($stack, array_pop($stack) - array_pop($stack));
  16.                 break;
  17.             case '*':
  18.                 array_push($stack, array_pop($stack) * array_pop($stack));
  19.                 break;
  20.             case '=':
  21.                 array_push($stack, array_pop($stack) === array_pop($stack));
  22.                 break;
  23.             case 'DUMP':
  24.                 var_dump($stack);
  25.                 break;
  26.             case 'DUP':
  27.                 array_push($stack, end($stack));
  28.                 break;
  29.  
  30.             default:
  31.                 array_push($stack, $chunk);
  32.  
  33.         }
  34.     }
  35. }
  36.  
  37. function ap(ArrayObject $arr) {
  38.     $last = $arr->count() - 1;
  39.     $item = $arr[$last];
  40.     $arr->offsetUnset($last);
  41.  
  42.     return $item;
  43. }
  44.  
  45.  
  46. echo 'Array: ', microtime(1) - $t, "\n";
  47.  
  48. $t = microtime(1);
  49.  
  50. for ($i = 0; $i < 10000; $i++) {
  51.     $stack = new ArrayObject(array(1,2));
  52.  
  53.     foreach (explode(' ', $input) as $chunk) {
  54.         switch ($chunk) {
  55.             case '+':
  56.                 $stack->append(ap($stack) + ap($stack));
  57.                 break;
  58.             case '-':
  59.                 $stack->append(ap($stack) - ap($stack));
  60.                 break;
  61.             case '*':
  62.                 $stack->append(ap($stack) * ap($stack));
  63.                 break;
  64.             case '=':
  65.                 $stack->append(ap($stack) === ap($stack));
  66.                 break;
  67.             case 'DUMP':
  68.                 var_dump($stack);
  69.                 break;
  70.             case 'DUP':
  71.                 $stack->append(end($stack));
  72.                 break;
  73.  
  74.             default:
  75.                 $stack->append($chunk);
  76.  
  77.         }
  78.     }
  79. }
  80.  
  81. echo 'Array Object: ', microtime(1) - $t, "\n";
  82.  
  83. $t = microtime(1);
  84.  
  85. for ($i = 0; $i < 10000; $i++) {
  86.     $stack = new SplStack();
  87.  
  88.     foreach (explode(' ', $input) as $chunk) {
  89.         switch ($chunk) {
  90.             case '+':
  91.                 $stack->push($stack->pop() + $stack->pop());
  92.                 break;
  93.             case '-':
  94.                 $stack->push($stack->pop() - $stack->pop());
  95.                 break;
  96.             case '*':
  97.                 $stack->push($stack->pop() * $stack->pop());
  98.                 break;
  99.             case '=':
  100.                 $stack->push($stack->pop() === $stack->pop());
  101.                 break;
  102.             case 'DUMP':
  103.                 var_dump($stack);
  104.                 break;
  105.             case 'DUP':
  106.                 $stack->push($stack->top());
  107.                 break;
  108.  
  109.             default:
  110.                 $stack->push($chunk);
  111.  
  112.         }
  113.     }
  114. }
  115.  
  116. echo 'SplStack: ', microtime(1) - $t, "\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement