SHARE
TWEET

Untitled

a guest Jan 17th, 2011 14 Never
  1. <?php
  2.  
  3.  
  4. $initial = 5;
  5. $unit = 10;
  6. $operation = '+';
  7.  
  8. function add($a, $b) { return $a + $b; }
  9. function sub($a, $b) { return $a - $b; }
  10.  
  11. $operations = array('+' => 'add', '-' => 'sub');
  12.  
  13. $count = 100000;
  14. $start = microtime(true);
  15. for($i = 0; $i < $count; $i++)
  16.         $operations[$operation]($initial, $unit);
  17. $end = microtime(true);
  18.  
  19. echo "took " . number_format((($end - $start)/$count), 15) . " microseconds\n";
  20.  
  21. $start = microtime(true);
  22. $str = "return $initial $operation $unit;";
  23. for($i = 0; $i < $count; $i++)
  24.         eval($str);
  25. $end = microtime(true);
  26.  
  27. echo "took " . number_format((($end - $start)/$count), 15) . " microseconds\n";
  28.  
  29. ?>
RAW Paste Data
Top