Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 10th, 2012  |  syntax: None  |  size: 1.10 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/bin/bash
  2. COUNT=1e6
  3. FUNCTIONS='
  4.         function res($x){
  5.                 //echo $x;
  6.         };
  7. '
  8. #CREATE_ARRAY='$a = array_fill(0, '$COUNT', "fofo");'
  9. CREATE_ARRAY='
  10.         $a = array();
  11.         for($i=0; $i<'$COUNT'; $i++){
  12.                 $a[$i] = $i;
  13.         };
  14. '
  15. CREATE_STRING='$content = str_repeat("x", 1e9);'
  16.  
  17. function run {
  18.         echo "== $1 =="
  19.         loop='
  20.                 $t1 = microtime(true);
  21.                 for($i=0; $i<10000000; $i++){
  22.                         '$2'
  23.                 };
  24.                 $t2 = microtime(true);
  25.                 echo $t2-$t1 . "\n";
  26.                 '
  27.         time php --no-php-ini -d memory_limit=2G -r "$FUNCTIONS $3 $loop"
  28.         echo
  29. }
  30.  
  31. function benchmark {
  32.         echo "--- $1 benchmarks ---"
  33.         run "ternary" "res($2?1:2);" "$3"
  34.         run "pre-condition" "\$cond = $2; res(\$cond?1:2);" "$3"
  35.         #run "pre-condition-booleanize" "\$cond = (bool)($2); res(\$cond?1:2);" "$3" # na toto nemám nervy
  36.         run "if-else" "if($2){res(1);}else{res(2);};" "$3"
  37. }
  38.  
  39. function array_benchmark {
  40.         benchmark "array: $1" "$2" "$CREATE_ARRAY"
  41. }
  42.  
  43. function string_benchmark {
  44.         benchmark "string: $1" "$2" "$CREATE_STRING"
  45. }
  46.  
  47. echo "--- PHP version ---"
  48. php -v
  49. string_benchmark 'false' '$content!==false'
  50. array_benchmark array-condition '$a'
  51. array_benchmark count 'count($a)'
  52. array_benchmark isset 'isset($a)'