@ 2025-06-10T16:03:55Z <?php
const N = 100_000;
const TYPE = 0;
function simple_alloc($id, $delete = false, $type = TYPE)
{
static $ids = [];
if (is_int($id)) {
$key = $id;
} else {
$key = array_search($id, $ids[$type] ?? [], true);
}
if ($delete) {
if ($key !== false) {
unset($ids[$type][$key]);
}
return $key;
}
if ($key === false) {
$key = count($ids[$type] ?? []);
$ids[$type][$key] = $id;
}
return $key;
}
function freelist_alloc($id, $delete = false, $type = TYPE)
{
static $ids = [];
static $freelist = [];
$freelist[$type] ??= [];
$ids[$type] ??= [];
if (is_int($id)) {
if ($delete) $freelist[$type][] = $id;
return $id;
}
$match = null;
foreach ($ids[$type] as $i => $arr) {
if (in_array($i, $freelist[$type], true)) continue;
if ($arr === $id) {
$match = $i;
if ($delete) $freelist[$type][] = $i;
break;
}
}
if ($match === null) {
$nextId = array_pop($freelist[$type]);
if ($nextId === null) {
$nextId = count($ids[$type]);
$ids[$type][] = $id;
} else {
$ids[$type][$nextId] = $id;
}
return $nextId;
}
return $match;
}
$t1 = microtime(true);
$live1 = [];
for ($i = 0; $i < N; $i++) {
if ($live1 && mt_rand(0, 1)) {
$k = array_rand($live1);
simple_alloc($live1[$k], true);
unset($live1[$k]);
} else {
$obj = 'obj-' . $i . '-' . mt_rand();
$id = simple_alloc($obj);
$live1[] = $id;
}
}
$dt_simple = microtime(true) - $t1;
$t2 = microtime(true);
$live2 = [];
for ($i = 0; $i < N; $i++) {
if ($live2 && mt_rand(0, 1)) {
$k = array_rand($live2);
freelist_alloc($live2[$k], true);
unset($live2[$k]);
} else {
$obj = 'obj-' . $i . '-' . mt_rand();
$id = freelist_alloc($obj);
$live2[] = $id;
}
}
$dt_freelist = microtime(true) - $t2;
// --- Results ---
echo "Random Allocation/Free Benchmark (N = " . number_format(N) . ")\n";
echo "Simple allocator: " . number_format($dt_simple, 4) . " sec\n";
echo "Freelist allocator: " . number_format($dt_freelist, 4) . " sec\n";
if ($dt_freelist > 0) {
echo "Simple is " . number_format($dt_freelist / $dt_simple, 2) . "x faster\n";
} else {
echo "\n";
}
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.4.8 0.036 0.720 18.59 8.4.7 0.023 0.765 17.59 8.4.6 0.018 0.853 17.90 8.4.5 0.018 0.923 17.80 8.4.4 0.022 0.989 17.40 8.4.3 0.022 0.776 17.49 8.4.2 0.022 0.864 17.32 8.4.1 0.021 0.809 17.60 8.3.22 0.019 0.987 18.97 8.3.21 0.005 0.620 16.84 8.3.20 0.022 1.977 16.84 8.3.19 0.016 1.983 16.84 8.3.18 0.021 0.808 16.84 8.3.17 0.019 1.091 16.84 8.3.16 0.022 1.125 16.84 8.3.15 0.018 0.527 16.84 8.3.14 0.029 1.821 16.84 8.3.13 0.018 1.983 16.84 8.3.12 0.017 0.877 16.84 8.3.11 0.018 1.720 16.84 8.3.10 0.024 1.019 16.84 8.3.9 0.018 1.362 16.84 8.3.8 0.016 0.555 16.84 8.3.7 0.020 0.697 16.84 8.3.6 0.020 1.899 16.84 8.3.5 0.031 0.552 16.84 8.3.4 0.027 1.384 17.86 8.3.3 0.015 1.091 17.86 8.3.2 0.012 0.800 17.64 8.3.1 0.016 0.704 17.92 8.3.0 0.015 1.271 17.57 8.2.28 0.017 1.061 16.84 8.2.27 0.016 0.478 16.84 8.2.26 0.020 0.877 16.84 8.2.25 0.013 0.584 16.84 8.2.24 0.016 1.651 16.84 8.2.23 0.016 0.489 16.84 8.2.22 0.017 1.564 16.84 8.2.21 0.007 0.606 16.84 8.2.20 0.007 1.706 18.31 8.2.19 0.004 0.714 18.45 8.2.18 0.005 1.155 18.41 8.2.17 0.007 1.544 19.52 8.2.16 0.011 1.009 19.34 8.2.15 0.009 0.652 19.36 8.2.14 0.006 1.171 19.47 8.2.13 0.004 0.667 19.38 8.2.12 0.006 0.763 19.40 8.2.11 0.011 0.926 19.43 8.2.10 0.008 1.679 19.55 8.2.9 0.012 1.133 17.89 8.2.8 0.006 0.981 19.43 8.2.7 0.003 0.914 19.42 8.2.6 0.007 1.229 19.39 8.2.5 0.013 1.041 19.54 8.2.4 0.011 1.091 17.76 8.2.3 0.015 0.981 17.89 8.2.2 0.011 1.702 17.81 8.2.1 0.010 0.815 19.59 8.2.0 0.005 1.167 19.43
preferences:dark mode live preview ace vim emacs key bindings
26.67 ms | 403 KiB | 5 Q