3v4l.org

run code in 300+ PHP versions simultaneously
<?php error_reporting(E_ALL & ~E_NOTICE); ini_set('display_errors', '1'); define('S', 15000); // list Size define('N', 10); // Number of iterations (for benchmark accuracy) // Comment the lines above and uncomment the lines below to pass the parameters in the command line //define('S', (int)$argv[1]); //define('N', (int)$argv[2]); printf("Generating %d elements... ", S); $tsStart = microtime(TRUE); $array = []; for ($i = 0; $i < S; $i ++) { $array[] = [ 'a' => rand(0, 100), 'f' => 0, 'f' => 0, 'l' => 61.60 ]; } printf("Done. Time: %f seconds.\n", microtime(TRUE) - $tsStart); $time = 0; for ($l = 0; $l < N; $l ++) { $ts = microtime(TRUE); $min = min(array_filter(array_column($array, 'a'))); $time += microtime(TRUE) - $ts; } printf("array_filter(): %9.2f seconds/%d iterations. %f seconds/iteration.\n", $time, N, $time/N); $time = 0; for ($l = 0; $l < N; $l ++) { $ts = microtime(TRUE); $min = null; foreach ($array as $val) { if ($min === null || ($val['a'] && $val['a'] < $min)) { $min = $val['a']; } } $time += microtime(TRUE) - $ts; } printf("foreach : %9.2f seconds/%d iterations. %f seconds/iteration.\n", $time, N, $time/N); $time = 0; for ($l = 0; $l < N; $l ++) { $ts = microtime(TRUE); $min = array_reduce($array, function ($min, $val) { return $min === null || ($val['a'] && $val['a'] < $min) ? $val['a'] : $min; }); $time += microtime(TRUE) - $ts; } printf("reduce @deceze: %9.2f seconds/%d iterations. %f seconds/iteration.\n", $time, N, $time/N); $time = 0; for ($l = 0; $l < N; $l ++) { $ts = microtime(TRUE); $min = array_reduce( $array, function($acc, array $item) { return min($acc, $item['a'] ?: INF); }, INF ); $time += microtime(TRUE) - $ts; } printf("reduce @axiac : %9.2f seconds/%d iterations. %f seconds/iteration.\n", $time, N, $time/N);

preferences:
27.65 ms | 414 KiB | 5 Q