Advertisement
Guest User

OnkelTem

a guest
Feb 13th, 2012
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.13 KB | None | 0 0
  1. <?php
  2.  
  3. define('COUNT', 1000);
  4.  
  5. $tests = array('<b>PHP native</b>', '<b>PHP eval</b>', '<b>PEAR RPN true-rpn</b>', '<b>PEAR RPN generic</b>');
  6. $results = array();
  7.  
  8. echo "<p>Running ".COUNT." times</p>";
  9.  
  10. echo "<p>Using $tests[0]: ";
  11. $start = microtime(true);
  12. for($i=0; $i<COUNT; $i++) {
  13.   $ret = 1230 + 1800 + $i;
  14. }
  15. $results[0] = microtime(true) - $start;
  16. echo $results[0]; echo 's</p>';
  17.  
  18. echo "<p>Using $tests[1]: ";
  19. $start = microtime(true);
  20. for($i=0; $i<COUNT; $i++) {
  21.   if (!($ret = eval('return 1230 + 1800 + '.$i.';'))) {
  22.     die('error in eval expression');
  23.   } else {
  24.     //print $ret . '<br/>';
  25.   }
  26. }
  27. $results[1] = microtime(true) - $start;
  28. echo $results[1]; echo 's</p>';
  29.  
  30. echo "<p>Using $tests[2]: ";
  31. $start = microtime(true);
  32. include_once 'Math/RPN.php';
  33. $rpn = new Math_Rpn();
  34. for($i=0; $i<COUNT; $i++) {
  35.   $expression = '1230 1800 + '.$i.' +';
  36.   if (($ret = $rpn->calculate($expression, 'deg', true)) == 'Syntax error') {
  37.     die('error in RPN expression');
  38.   } else {
  39.     //print $ret . '<br/>';
  40.   }
  41. }
  42. $results[2] = microtime(true) - $start;
  43. echo $results[2]; echo 's</p>';
  44.  
  45. echo "<p>Using $tests[2]: ";
  46. $start = microtime(true);
  47. include_once 'Math/RPN.php';
  48. $start = microtime(true);
  49. for($i=0; $i<COUNT; $i++) {
  50.   $expression = '1230 + 1800 + '.$i;
  51.   $rpn = new Math_Rpn();
  52.   if (($ret = $rpn->calculate($expression, 'deg', false)) == 'Syntax error') {
  53.     die('error in RPN expression');
  54.   } else {
  55.     //print $ret . '<br/>';
  56.   }
  57. }
  58. $results[3] = microtime(true) - $start;
  59. echo $results[3]; echo 's</p>';
  60.  
  61. echo "<p>RESULTS</p>";
  62.  
  63. echo "<p>$tests[1] is slower then $tests[0] in ".round($results[1]/$results[0])." times</p>";
  64. echo "<p>$tests[2] is slower then $tests[0] in ".round($results[2]/$results[0])." times</p>";
  65. echo "<p>$tests[3] is slower then $tests[0] in ".round($results[3]/$results[0])." times</p>";
  66. echo "<p>$tests[2] is slower then $tests[1] in ".round($results[2]/$results[1])." times</p>";
  67. echo "<p>$tests[3] is slower then $tests[1] in ".round($results[3]/$results[1])." times</p>";
  68. echo "<p>$tests[3] is slower then $tests[2] in ".round($results[3]/$results[2])." times</p>";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement