3v4l.org

run code in 200+ php & hhvm versions
<?php // test a simple array_map in the real world. function test_array_map($data){ return array_map(function($row){ return array( 'productId' => $row['id'] + 1, 'productName' => $row['name'], 'desc' => $row['remark'] ); }, $data); } // Another with local variable $i function test_array_map_use_local($data){ $i = 0; return array_map(function($row) use ($i) { $i++; return array( 'productId' => $row['id'] + $i, 'productName' => $row['name'], 'desc' => $row['remark'] ); }, $data); } // test a simple foreach in the real world function test_foreach($data){ $result = array(); foreach ($data as $row) { $tmp = array(); $tmp['productId'] = $row['id'] + 1; $tmp['productName'] = $row['name']; $tmp['desc'] = $row['remark']; $result[] = $tmp; } return $result; } // Another with local variable $i function test_foreach_use_local($data){ $result = array(); $i = 0; foreach ($data as $row) { $i++; $tmp = array(); $tmp['productId'] = $row['id'] + $i; $tmp['productName'] = $row['name']; $tmp['desc'] = $row['remark']; $result[] = $tmp; } return $result; } $data = array_fill(0, 10000, array( 'id' => 1, 'name' => 'test', 'remark' => 'ok' )); $tests = array( 'array_map' => array(), 'foreach' => array(), 'array_map_use_local' => array(), 'foreach_use_local' => array(), ); for ($i = 0; $i < 100; $i++){ foreach ($tests as $testName => &$records) { $start = microtime(true); call_user_func("test_$testName", $data); $delta = microtime(true) - $start; $records[] = $delta; } } // output result: foreach ($tests as $name => &$records) { printf('%.4f : %s '.PHP_EOL, array_sum($records) / count($records), $name); }

Abusive script

This script was stopped while abusing our resources

Output for 7.2.10, 7.2.12
0.0035 : array_map 0.0034 : foreach 0.0031 : array_map_use_local 0.0035 : foreach_use_local
Output for 7.2.11
0.0043 : array_map 0.0046 : foreach 0.0044 : array_map_use_local 0.0048 : foreach_use_local
Output for 7.2.9
0.0027 : array_map 0.0028 : foreach 0.0026 : array_map_use_local 0.0029 : foreach_use_local
Output for 7.2.8
0.0029 : array_map 0.0027 : foreach 0.0024 : array_map_use_local 0.0027 : foreach_use_local
Output for 7.2.7
0.0031 : array_map 0.0038 : foreach 0.0034 : array_map_use_local 0.0037 : foreach_use_local
Output for 7.2.6
0.0044 : array_map 0.0046 : foreach 0.0044 : array_map_use_local 0.0049 : foreach_use_local
Output for 7.2.5
0.0028 : array_map 0.0029 : foreach 0.0028 : array_map_use_local 0.0029 : foreach_use_local
Output for 7.2.4
0.0052 : array_map 0.0050 : foreach 0.0048 : array_map_use_local 0.0054 : foreach_use_local
Output for 7.2.3
0.0031 : array_map 0.0032 : foreach 0.0030 : array_map_use_local 0.0034 : foreach_use_local
Output for 7.2.2
0.0027 : array_map 0.0027 : foreach 0.0026 : array_map_use_local 0.0028 : foreach_use_local
Output for 7.2.1
0.0035 : array_map 0.0033 : foreach 0.0032 : array_map_use_local 0.0034 : foreach_use_local
Output for 7.2.0
0.0028 : array_map 0.0029 : foreach 0.0026 : array_map_use_local 0.0029 : foreach_use_local
Output for 7.1.24
0.0032 : array_map 0.0032 : foreach 0.0028 : array_map_use_local 0.0033 : foreach_use_local
Output for 7.1.23
0.0029 : array_map 0.0031 : foreach 0.0025 : array_map_use_local 0.0032 : foreach_use_local
Output for 7.1.22
0.0031 : array_map 0.0031 : foreach 0.0028 : array_map_use_local 0.0033 : foreach_use_local
Output for 7.1.21
0.0029 : array_map 0.0029 : foreach 0.0026 : array_map_use_local 0.0030 : foreach_use_local
Output for 7.1.20
0.0026 : array_map 0.0026 : foreach 0.0023 : array_map_use_local 0.0027 : foreach_use_local
Output for 7.1.19
0.0035 : array_map 0.0034 : foreach 0.0029 : array_map_use_local 0.0035 : foreach_use_local
Output for 7.1.18
0.0026 : array_map 0.0028 : foreach 0.0026 : array_map_use_local 0.0032 : foreach_use_local
Output for 7.1.17
0.0033 : array_map 0.0036 : foreach 0.0031 : array_map_use_local 0.0036 : foreach_use_local
Output for 7.1.16
0.0053 : array_map 0.0055 : foreach 0.0051 : array_map_use_local 0.0058 : foreach_use_local
Output for 7.1.15
0.0031 : array_map 0.0034 : foreach 0.0031 : array_map_use_local 0.0037 : foreach_use_local
Output for 7.1.14
0.0043 : array_map 0.0039 : foreach 0.0036 : array_map_use_local 0.0040 : foreach_use_local
Output for 7.1.13
0.0045 : array_map 0.0041 : foreach 0.0043 : array_map_use_local 0.0043 : foreach_use_local
Output for 7.1.12
0.0027 : array_map 0.0028 : foreach 0.0023 : array_map_use_local 0.0027 : foreach_use_local
Output for 7.1.11
0.0031 : array_map 0.0028 : foreach 0.0027 : array_map_use_local 0.0028 : foreach_use_local
Output for 7.1.10
0.0026 : array_map 0.0027 : foreach 0.0024 : array_map_use_local 0.0027 : foreach_use_local
Output for 7.1.0
0.0028 : array_map 0.0033 : foreach 0.0033 : array_map_use_local 0.0033 : foreach_use_local
Output for 7.0.32
0.0037 : array_map 0.0038 : foreach 0.0036 : array_map_use_local 0.0038 : foreach_use_local
Output for 7.0.31
0.0033 : array_map 0.0032 : foreach 0.0032 : array_map_use_local 0.0033 : foreach_use_local
Output for 7.0.30
0.0028 : array_map 0.0030 : foreach 0.0029 : array_map_use_local 0.0030 : foreach_use_local
Output for 7.0.29
0.0052 : array_map 0.0054 : foreach 0.0049 : array_map_use_local 0.0054 : foreach_use_local
Output for 7.0.28
0.0033 : array_map 0.0032 : foreach 0.0031 : array_map_use_local 0.0032 : foreach_use_local
Output for 7.0.27
0.0037 : array_map 0.0036 : foreach 0.0034 : array_map_use_local 0.0037 : foreach_use_local
Output for 7.0.26
0.0032 : array_map 0.0033 : foreach 0.0032 : array_map_use_local 0.0033 : foreach_use_local
Output for 7.0.25
0.0025 : array_map 0.0027 : foreach 0.0026 : array_map_use_local 0.0028 : foreach_use_local
Output for 7.0.24
0.0029 : array_map 0.0029 : foreach 0.0030 : array_map_use_local 0.0030 : foreach_use_local
Output for 7.0.14
0.0035 : array_map 0.0037 : foreach 0.0040 : array_map_use_local 0.0038 : foreach_use_local
Output for 7.0.13
0.0034 : array_map 0.0040 : foreach 0.0041 : array_map_use_local 0.0041 : foreach_use_local
Output for 7.0.12
0.0033 : array_map 0.0038 : foreach 0.0037 : array_map_use_local 0.0038 : foreach_use_local
Output for 7.0.11
0.0029 : array_map 0.0036 : foreach 0.0033 : array_map_use_local 0.0035 : foreach_use_local
Output for 7.0.10
0.0031 : array_map 0.0034 : foreach 0.0032 : array_map_use_local 0.0034 : foreach_use_local
Output for 7.0.9
0.0033 : array_map 0.0039 : foreach 0.0038 : array_map_use_local 0.0041 : foreach_use_local
Output for 7.0.8
0.0030 : array_map 0.0033 : foreach 0.0033 : array_map_use_local 0.0034 : foreach_use_local
Output for 7.0.7
0.0031 : array_map 0.0037 : foreach 0.0035 : array_map_use_local 0.0041 : foreach_use_local
Output for 7.0.6
0.0034 : array_map 0.0036 : foreach 0.0037 : array_map_use_local 0.0036 : foreach_use_local
Output for 7.0.5
0.0034 : array_map 0.0034 : foreach 0.0037 : array_map_use_local 0.0034 : foreach_use_local
Output for 7.0.4
0.0032 : array_map 0.0032 : foreach 0.0036 : array_map_use_local 0.0034 : foreach_use_local
Output for 7.0.3
0.0033 : array_map 0.0032 : foreach 0.0037 : array_map_use_local 0.0033 : foreach_use_local
Output for 7.0.2
0.0033 : array_map 0.0034 : foreach 0.0036 : array_map_use_local 0.0034 : foreach_use_local
Output for 7.0.1
0.0033 : array_map 0.0034 : foreach 0.0041 : array_map_use_local 0.0035 : foreach_use_local
Output for 7.0.0
0.0033 : array_map 0.0033 : foreach 0.0036 : array_map_use_local 0.0034 : foreach_use_local
Output for hhvm-3.22.0
0.0050 : array_map 0.0053 : foreach 0.0053 : array_map_use_local 0.0055 : foreach_use_local
Output for hhvm-3.21.3
0.0048 : array_map 0.0048 : foreach 0.0054 : array_map_use_local 0.0051 : foreach_use_local
Output for 5.6.0 - 5.6.38, hhvm-3.18.5

Process exited with code 137.