Advertisement
IchHabRecht

[PHP] Benchmark array functions

Feb 26th, 2014
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.80 KB | None | 0 0
  1. <?php
  2.  
  3. $runs = 10000;
  4.  
  5. function benchmark1() {
  6.     global $runs;
  7.  
  8.     for ($i = 0; $i < $runs; $i++) {
  9.         $array = array_fill(0, 10, 'array');
  10.         $arrayCount = count($array);
  11.         for ($j = 0; $j < $arrayCount; $j++) {
  12.             $dummy = $j . ' => ' . $array[$j];
  13.         }
  14.     }
  15. }
  16.  
  17. function benchmark2() {
  18.     global $runs;
  19.  
  20.     for ($i = 0; $i < $runs; $i++) {
  21.         $array = array_fill(0, 10, 'array');
  22.         foreach ($array as $key => $value) {
  23.             $dummy = $key . ' => ' . $value;
  24.         }
  25.         unset($key, $value);
  26.     }
  27. }
  28.  
  29. function benchmark3() {
  30.     global $runs;
  31.  
  32.     $dummy = '';
  33.     $function = function ($value) use ($dummy) {
  34.         $dummy = $value;
  35.     };
  36.  
  37.     for ($i = 0; $i < $runs; $i++) {
  38.         $array = array_fill(0, 10, 'array');
  39.         array_map($function, $array);
  40.     }
  41. }
  42.  
  43. function benchmark4() {
  44.     global $runs;
  45.  
  46.     $dummy = '';
  47.     $function = function ($value) use ($dummy) {
  48.         $dummy = $value;
  49.     };
  50.  
  51.     for ($i = 0; $i < $runs; $i++) {
  52.         $array = array_fill(0, 10, 'array');
  53.         array_walk($array, $function);
  54.     }
  55. }
  56.  
  57. function run_benchmark($benchmarkFunction) {
  58.     $start_time = microtime(TRUE);
  59.  
  60.     call_user_func($benchmarkFunction);
  61.  
  62.     $end_time = microtime(TRUE);
  63.  
  64.     return $end_time - $start_time;
  65. }
  66.  
  67. function run_benchmark_times($benchmarkFunction, $times = 10) {
  68.     $totalTime = 0;
  69.     $benchmark = '';
  70.     for ($i = 0; $i < $times; ++$i) {
  71.         $runTime = run_benchmark($benchmarkFunction);
  72.         $benchmark .= $i . ': ' . $runTime . '<br />';
  73.         $totalTime += $runTime;
  74.     }
  75.     $averageTime = $totalTime / $times;
  76.     $benchmark .= 'Average:' . $averageTime . '<br />';
  77.  
  78.     return $benchmark;
  79. }
  80.  
  81. echo 'for:<br />' . run_benchmark_times('benchmark1') . '<br /><br />';
  82.  
  83. echo 'foreach:<br />' . run_benchmark_times('benchmark2') . '<br /><br />';
  84.  
  85. echo 'array_map:<br />' . run_benchmark_times('benchmark3') . '<br /><br />';
  86.  
  87. echo 'array_walk:<br />' . run_benchmark_times('benchmark4') . '<br /><br />';
  88.  
  89.  
  90.  
  91. for:
  92. 0: 1.0302460193634
  93. 1: 0.65146803855896
  94. 2: 0.66136193275452
  95. 3: 0.64415311813354
  96. 4: 0.6526780128479
  97. 5: 0.6699538230896
  98. 6: 0.66441917419434
  99. 7: 0.64700198173523
  100. 8: 0.67700600624084
  101. 9: 0.65721106529236
  102. Average:0.69554991722107
  103.  
  104.  
  105. foreach:
  106. 0: 0.52113389968872
  107. 1: 0.51127409934998
  108. 2: 0.52082180976868
  109. 3: 0.54333400726318
  110. 4: 0.5436429977417
  111. 5: 0.48052191734314
  112. 6: 0.45839810371399
  113. 7: 0.46226000785828
  114. 8: 0.49614500999451
  115. 9: 0.53473901748657
  116. Average:0.50722708702087
  117.  
  118.  
  119. array_map:
  120. 0: 4.6220200061798
  121. 1: 4.6417779922485
  122. 2: 4.5292210578918
  123. 3: 4.6383039951324
  124. 4: 4.688591003418
  125. 5: 5.0692970752716
  126. 6: 4.909529209137
  127. 7: 5.1962251663208
  128. 8: 5.9621770381927
  129. 9: 7.1543881893158
  130. Average:5.1411530733109
  131.  
  132.  
  133. array_walk:
  134. 0: 6.82506108284
  135. 1: 5.1182498931885
  136. 2: 5.0953998565674
  137. 3: 7.288468837738
  138. 4: 6.3256978988647
  139. 5: 5.2610161304474
  140. 6: 6.2699329853058
  141. 7: 5.3987231254578
  142. 8: 6.5060670375824
  143. 9: 5.9784679412842
  144. Average:6.0067084789276
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement