Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $runs = 10000;
- function benchmark1() {
- global $runs;
- for ($i = 0; $i < $runs; $i++) {
- $array = array_fill(0, 10, 'array');
- $arrayCount = count($array);
- for ($j = 0; $j < $arrayCount; $j++) {
- $dummy = $j . ' => ' . $array[$j];
- }
- }
- }
- function benchmark2() {
- global $runs;
- for ($i = 0; $i < $runs; $i++) {
- $array = array_fill(0, 10, 'array');
- foreach ($array as $key => $value) {
- $dummy = $key . ' => ' . $value;
- }
- unset($key, $value);
- }
- }
- function benchmark3() {
- global $runs;
- $dummy = '';
- $function = function ($value) use ($dummy) {
- $dummy = $value;
- };
- for ($i = 0; $i < $runs; $i++) {
- $array = array_fill(0, 10, 'array');
- array_map($function, $array);
- }
- }
- function benchmark4() {
- global $runs;
- $dummy = '';
- $function = function ($value) use ($dummy) {
- $dummy = $value;
- };
- for ($i = 0; $i < $runs; $i++) {
- $array = array_fill(0, 10, 'array');
- array_walk($array, $function);
- }
- }
- function run_benchmark($benchmarkFunction) {
- $start_time = microtime(TRUE);
- call_user_func($benchmarkFunction);
- $end_time = microtime(TRUE);
- return $end_time - $start_time;
- }
- function run_benchmark_times($benchmarkFunction, $times = 10) {
- $totalTime = 0;
- $benchmark = '';
- for ($i = 0; $i < $times; ++$i) {
- $runTime = run_benchmark($benchmarkFunction);
- $benchmark .= $i . ': ' . $runTime . '<br />';
- $totalTime += $runTime;
- }
- $averageTime = $totalTime / $times;
- $benchmark .= 'Average:' . $averageTime . '<br />';
- return $benchmark;
- }
- echo 'for:<br />' . run_benchmark_times('benchmark1') . '<br /><br />';
- echo 'foreach:<br />' . run_benchmark_times('benchmark2') . '<br /><br />';
- echo 'array_map:<br />' . run_benchmark_times('benchmark3') . '<br /><br />';
- echo 'array_walk:<br />' . run_benchmark_times('benchmark4') . '<br /><br />';
- for:
- 0: 1.0302460193634
- 1: 0.65146803855896
- 2: 0.66136193275452
- 3: 0.64415311813354
- 4: 0.6526780128479
- 5: 0.6699538230896
- 6: 0.66441917419434
- 7: 0.64700198173523
- 8: 0.67700600624084
- 9: 0.65721106529236
- Average:0.69554991722107
- foreach:
- 0: 0.52113389968872
- 1: 0.51127409934998
- 2: 0.52082180976868
- 3: 0.54333400726318
- 4: 0.5436429977417
- 5: 0.48052191734314
- 6: 0.45839810371399
- 7: 0.46226000785828
- 8: 0.49614500999451
- 9: 0.53473901748657
- Average:0.50722708702087
- array_map:
- 0: 4.6220200061798
- 1: 4.6417779922485
- 2: 4.5292210578918
- 3: 4.6383039951324
- 4: 4.688591003418
- 5: 5.0692970752716
- 6: 4.909529209137
- 7: 5.1962251663208
- 8: 5.9621770381927
- 9: 7.1543881893158
- Average:5.1411530733109
- array_walk:
- 0: 6.82506108284
- 1: 5.1182498931885
- 2: 5.0953998565674
- 3: 7.288468837738
- 4: 6.3256978988647
- 5: 5.2610161304474
- 6: 6.2699329853058
- 7: 5.3987231254578
- 8: 6.5060670375824
- 9: 5.9784679412842
- Average:6.0067084789276
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement