3v4l.org

run code in 300+ PHP versions simultaneously
<?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"; }
Output for 8.4.8
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0395 sec Freelist allocator: 0.6788 sec Simple is 17.18x faster
Output for 8.4.7
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0209 sec Freelist allocator: 0.7409 sec Simple is 35.39x faster
Output for 8.4.6
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0189 sec Freelist allocator: 0.8290 sec Simple is 43.81x faster
Output for 8.4.5
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0211 sec Freelist allocator: 0.8972 sec Simple is 42.48x faster
Output for 8.4.4
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0188 sec Freelist allocator: 0.9684 sec Simple is 51.45x faster
Output for 8.4.3
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0216 sec Freelist allocator: 0.7517 sec Simple is 34.82x faster
Output for 8.4.2
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0217 sec Freelist allocator: 0.8377 sec Simple is 38.68x faster
Output for 8.4.1
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0196 sec Freelist allocator: 0.7855 sec Simple is 40.17x faster
Output for 8.3.22
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0186 sec Freelist allocator: 0.9699 sec Simple is 52.12x faster
Output for 8.3.21
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0214 sec Freelist allocator: 0.5913 sec Simple is 27.62x faster
Output for 8.3.13, 8.3.19 - 8.3.20

Process exited with code 137.
Output for 8.3.18
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0227 sec Freelist allocator: 0.7803 sec Simple is 34.30x faster
Output for 8.3.17
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0172 sec Freelist allocator: 1.0684 sec Simple is 62.06x faster
Output for 8.3.16
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0199 sec Freelist allocator: 1.1033 sec Simple is 55.36x faster
Output for 8.3.15
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0191 sec Freelist allocator: 0.5029 sec Simple is 26.37x faster
Output for 8.3.14
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0247 sec Freelist allocator: 1.7992 sec Simple is 72.78x faster
Output for 8.3.12
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0172 sec Freelist allocator: 0.8564 sec Simple is 49.76x faster
Output for 8.3.11
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0189 sec Freelist allocator: 1.6947 sec Simple is 89.78x faster
Output for 8.3.10
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0187 sec Freelist allocator: 0.9966 sec Simple is 53.20x faster
Output for 8.3.9
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0191 sec Freelist allocator: 1.3359 sec Simple is 70.08x faster
Output for 8.3.8
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0173 sec Freelist allocator: 0.5299 sec Simple is 30.65x faster
Output for 8.3.7
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0197 sec Freelist allocator: 0.6716 sec Simple is 34.12x faster
Output for 8.3.6
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0193 sec Freelist allocator: 1.8755 sec Simple is 96.94x faster
Output for 8.3.5
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0196 sec Freelist allocator: 0.5254 sec Simple is 26.75x faster
Output for 8.3.4
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0307 sec Freelist allocator: 1.3429 sec Simple is 43.68x faster
Output for 8.3.3
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0201 sec Freelist allocator: 1.0674 sec Simple is 53.05x faster
Output for 8.3.2
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0239 sec Freelist allocator: 0.7673 sec Simple is 32.11x faster
Output for 8.3.1
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0169 sec Freelist allocator: 0.6831 sec Simple is 40.43x faster
Output for 8.3.0
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0341 sec Freelist allocator: 1.2312 sec Simple is 36.11x faster
Output for 8.2.28
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0211 sec Freelist allocator: 1.0358 sec Simple is 49.19x faster
Output for 8.2.27
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0172 sec Freelist allocator: 0.4568 sec Simple is 26.50x faster
Output for 8.2.26
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0217 sec Freelist allocator: 0.8524 sec Simple is 39.36x faster
Output for 8.2.25
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0189 sec Freelist allocator: 0.5578 sec Simple is 29.58x faster
Output for 8.2.24
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0181 sec Freelist allocator: 1.6283 sec Simple is 89.79x faster
Output for 8.2.23
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0225 sec Freelist allocator: 0.4624 sec Simple is 20.58x faster
Output for 8.2.22
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0204 sec Freelist allocator: 1.5351 sec Simple is 75.36x faster
Output for 8.2.21
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0171 sec Freelist allocator: 0.5855 sec Simple is 34.23x faster
Output for 8.2.20
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0363 sec Freelist allocator: 1.6583 sec Simple is 45.73x faster
Output for 8.2.19
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0188 sec Freelist allocator: 0.6895 sec Simple is 36.71x faster
Output for 8.2.18
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0181 sec Freelist allocator: 1.1315 sec Simple is 62.36x faster
Output for 8.2.17
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0402 sec Freelist allocator: 1.4964 sec Simple is 37.21x faster
Output for 8.2.16
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0398 sec Freelist allocator: 0.9656 sec Simple is 24.28x faster
Output for 8.2.15
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0173 sec Freelist allocator: 0.6337 sec Simple is 36.62x faster
Output for 8.2.14
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0170 sec Freelist allocator: 1.1489 sec Simple is 67.63x faster
Output for 8.2.13
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0208 sec Freelist allocator: 0.6395 sec Simple is 30.69x faster
Output for 8.2.12
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0182 sec Freelist allocator: 0.7411 sec Simple is 40.73x faster
Output for 8.2.11
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0332 sec Freelist allocator: 0.8854 sec Simple is 26.66x faster
Output for 8.2.10
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0199 sec Freelist allocator: 1.6577 sec Simple is 83.43x faster
Output for 8.2.9
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0349 sec Freelist allocator: 1.0925 sec Simple is 31.35x faster
Output for 8.2.8
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0179 sec Freelist allocator: 0.9589 sec Simple is 53.72x faster
Output for 8.2.7
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0177 sec Freelist allocator: 0.8893 sec Simple is 50.13x faster
Output for 8.2.6
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0196 sec Freelist allocator: 1.2055 sec Simple is 61.42x faster
Output for 8.2.5
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0354 sec Freelist allocator: 0.9999 sec Simple is 28.21x faster
Output for 8.2.4
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0460 sec Freelist allocator: 1.0365 sec Simple is 22.52x faster
Output for 8.2.3
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0366 sec Freelist allocator: 0.9415 sec Simple is 25.70x faster
Output for 8.2.2
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0336 sec Freelist allocator: 1.6608 sec Simple is 49.36x faster
Output for 8.2.1
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0325 sec Freelist allocator: 0.7736 sec Simple is 23.82x faster
Output for 8.2.0
Random Allocation/Free Benchmark (N = 100,000) Simple allocator: 0.0199 sec Freelist allocator: 1.1415 sec Simple is 57.46x faster

preferences:
46.65 ms | 490 KiB | 5 Q