@ 2024-01-15T05:22:31Z <?php
$testList = [
[
'array' => [],
'expect' => true,
],
[
'array' => ['a', 'b', 'c'],
'expect' => true,
],
[
'array' => ["0" => 'a', "1" => 'b', "2" => 'c'],
'expect' => true,
],
[
'array' => ["1" => 'a', "0" => 'b', "2" => 'c'],
'expect' => false,
],
[
'array' => ["a" => 'a', "b" => 'b', "c" => 'c'],
'expect' => false,
],
];
function displayBenchmark($function)
{
global $testList;
echo '<h5>use function <code>' . $function . '</code></h5>';
$i = 0;
$startTime = microtime(true);
for ($i = 0; $i <= 1000; ++$i) {
foreach ($testList as $testItem) {
$result = $function($testItem['array']);
assert($result === $testItem['expect'], 'Unexpect matched on index ' . $i);
++$i;
}
}
$totalTime = microtime(true) - $startTime;
echo 'total time: ' . var_export($totalTime, true) . '<br>';
}
function array_is_list1(array $arr)
{
if ($arr === []) {
return true;
}
return array_keys($arr) === range(0, count($arr) - 1);
}
function array_is_list2(array $array): bool
{
$i = -1;
foreach ($array as $k => $v) {
++$i;
if ($k !== $i) {
return false;
}
}
return true;
}
class Benchmark
{
private static $max, $memory;
public static function memoryTick()
{
self::$memory = memory_get_usage() - self::$memory;
self::$max = self::$memory>self::$max?self::$memory:self::$max;
self::$memory = memory_get_usage();
}
public static function benchmarkMemory(callable $function, $args=null)
{
declare(ticks=1);
self::$memory = memory_get_usage();
self::$max = 0;
register_tick_function('call_user_func_array', ['Benchmark', 'memoryTick'], []);
$result = is_array($args)?
call_user_func_array($function, $args):
call_user_func($function);
unregister_tick_function('call_user_func_array');
return [
'memory' => self::$max
];
}
}
var_dump(Benchmark::benchmarkMemory('displayBenchmark', ['array_is_list']));
var_dump(Benchmark::benchmarkMemory('displayBenchmark', ['array_is_list1']));
var_dump(Benchmark::benchmarkMemory('displayBenchmark', ['array_is_list2']));
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 8.3.11 0.009 0.012 18.42 8.3.1 0.007 0.014 21.88 8.3.0 0.011 0.007 18.84 8.2.14 0.007 0.018 20.47 8.2.13 0.003 0.017 20.53 8.2.12 0.009 0.006 18.82 8.2.11 0.013 0.009 19.28 8.2.10 0.008 0.008 19.05 8.2.9 0.003 0.013 19.00 8.2.8 0.010 0.006 18.95 8.2.7 0.009 0.009 18.88 8.2.6 0.010 0.013 18.74 8.2.5 0.014 0.007 18.64 8.2.4 0.019 0.000 18.89 8.2.3 0.006 0.009 18.68 8.2.2 0.003 0.006 18.69 8.2.1 0.013 0.007 18.78 8.2.0 0.009 0.000 18.53 8.1.27 0.015 0.004 22.19 8.1.26 0.013 0.006 22.12 8.1.25 0.010 0.010 22.02 8.1.24 0.009 0.000 18.75 8.1.23 0.009 0.009 18.62 8.1.22 0.009 0.006 18.82 8.1.21 0.010 0.006 18.55 8.1.20 0.007 0.014 18.81 8.1.19 0.014 0.007 18.80 8.1.18 0.013 0.009 18.51 8.1.17 0.007 0.010 18.52 8.1.16 0.003 0.006 18.60 8.1.15 0.004 0.012 18.49 8.1.14 0.009 0.006 18.51 8.1.13 0.009 0.000 18.72 8.1.12 0.008 0.004 18.53 8.1.11 0.006 0.003 18.54 8.1.10 0.006 0.003 18.57 8.1.9 0.009 0.000 18.66 8.1.8 0.009 0.000 18.58 8.1.7 0.000 0.008 18.47 8.1.6 0.003 0.006 18.73 8.1.5 0.006 0.003 18.63 8.1.4 0.008 0.000 18.75 8.1.3 0.009 0.000 18.87 8.1.2 0.003 0.006 18.69 8.1.1 0.003 0.006 18.61 8.1.0 0.008 0.000 18.57 7.4.33 0.017 0.002 17.30 7.2.16 0.018 0.009 22.96
preferences:dark mode live preview
28.29 ms | 406 KiB | 6 Q