daily pastebin goal
29%
SHARE
TWEET

Untitled

a guest May 16th, 2018 100 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /**
  3.  *
  4.  */
  5.  
  6. ini_set('memory_limit', -1);
  7. //putenv('summarize=1');
  8.  
  9. function snap($name, $init_func, $func, $summarize = false)
  10. {
  11.     $data = $init_func();
  12.     if (getenv('summarize')) $summarize = true;
  13.  
  14.     if (!$summarize) {
  15.         echo '##-----------------------------------------', PHP_EOL;
  16.         echo '## The test of "', $name, '"', PHP_EOL;
  17.         echo '##-----------------------------------------', PHP_EOL;
  18.     }
  19.     $start = microtime(true);
  20.     $func($data);
  21.     $end = microtime(true);
  22.     if (!$summarize) {
  23.     echo "## Start:\t", $start, PHP_EOL;
  24.     echo "## End:  \t", $end, PHP_EOL;
  25.     echo "## Total:\t", $end - $start, PHP_EOL;
  26.     echo '##-----------------------------------------', PHP_EOL, PHP_EOL;
  27.     }
  28.     else {
  29.         echo $name, "\t", $end - $start, PHP_EOL;
  30.     }
  31. }
  32.  
  33. function array_init()
  34. {
  35.     return range(0, 1000000);
  36. }
  37.  
  38. // foreach
  39. snap('foreach (Read)', 'array_init', function($a)
  40. {
  41.     foreach ($a as $k => $v) $k;
  42. });
  43.  
  44. // for
  45. snap('for (Read)', 'array_init', function($a)
  46. {
  47.     $key = array_keys($a);
  48.     for ($i = 0, $end = count($key); $i < $end; ++$i) $a[$key[$i]];
  49. });
  50.  
  51. // each
  52. snap('each,list (Read)', 'array_init', function($a)
  53. {
  54.     reset($a);
  55.     while(list($k, $v) = each($a)) $v;
  56. });
  57.  
  58.  
  59. // foreach
  60. snap('foreach as key (Write)', 'array_init', function($a)
  61. {
  62.     foreach ($a as $k => $v) $a[$k] .= 'a';
  63. });
  64.  
  65. // foreach
  66. snap('foreach ref (Write)', 'array_init', function($a)
  67. {
  68.     foreach ($a as $k => &$v) $v .= 'a';
  69. });
  70.  
  71. // for
  72. snap('for (Write)', 'array_init', function($a)
  73. {
  74.     $key = array_keys($a);
  75.     for ($i = 0, $end = count($key); $i < $end; ++$i) $a[$key[$i]] = 'a';
  76. });
  77.  
  78. // each
  79. snap('each,list (Write)', 'array_init', function($a)
  80. {
  81.     reset($a);
  82.     while(list($k, $v) = each($a)) $a[$k] .= 'a';
  83. });
  84.  
  85. /*
  86.  * ##-----------------------------------------
  87.  * ## The test of "foreach (Read)"
  88.  * ##-----------------------------------------
  89.  * ## Start:       1259729358.9
  90.  * ## End:         1259729359.25
  91.  * ## Total:       0.348405122757
  92.  * ##-----------------------------------------
  93.  *
  94.  * ##-----------------------------------------
  95.  * ## The test of "for (Read)"
  96.  * ##-----------------------------------------
  97.  * ## Start:       1259729359.51
  98.  * ## End:         1259729360.11
  99.  * ## Total:       0.599618196487
  100.  * ##-----------------------------------------
  101.  *
  102.  * ##-----------------------------------------
  103.  * ## The test of "each,list (Read)"
  104.  * ##-----------------------------------------
  105.  * ## Start:       1259729360.35
  106.  * ## End:         1259729362.77
  107.  * ## Total:       2.41448998451
  108.  * ##-----------------------------------------
  109.  *
  110.  * ##-----------------------------------------
  111.  * ## The test of "foreach as key (Write)"
  112.  * ##-----------------------------------------
  113.  * ## Start:       1259729363.04
  114.  * ## End:         1259729364.13
  115.  * ## Total:       1.0903198719
  116.  * ##-----------------------------------------
  117.  *
  118.  * ##-----------------------------------------
  119.  * ## The test of "foreach ref (Write)"
  120.  * ##-----------------------------------------
  121.  * ## Start:       1259729364.43
  122.  * ## End:         1259729365.31
  123.  * ## Total:       0.886390924454
  124.  * ##-----------------------------------------
  125.  *
  126.  * ##-----------------------------------------
  127.  * ## The test of "for (Write)"
  128.  * ##-----------------------------------------
  129.  * ## Start:       1259729365.61
  130.  * ## End:         1259729366.65
  131.  * ## Total:       1.03505206108
  132.  * ##-----------------------------------------
  133.  *
  134.  * ##-----------------------------------------
  135.  * ## The test of "each,list (Write)"
  136.  * ##-----------------------------------------
  137.  * ## Start:       1259729367.01
  138.  * ## End:         1259729370.15
  139.  * ## Total:       3.14639210701
  140.  * ##-----------------------------------------
  141.  */
RAW Paste Data
Top