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"; }

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).

VersionSystem time (s)User time (s)Memory (MiB)
8.4.80.0360.72018.59
8.4.70.0230.76517.59
8.4.60.0180.85317.90
8.4.50.0180.92317.80
8.4.40.0220.98917.40
8.4.30.0220.77617.49
8.4.20.0220.86417.32
8.4.10.0210.80917.60
8.3.220.0190.98718.97
8.3.210.0050.62016.84
8.3.200.0221.97716.84
8.3.190.0161.98316.84
8.3.180.0210.80816.84
8.3.170.0191.09116.84
8.3.160.0221.12516.84
8.3.150.0180.52716.84
8.3.140.0291.82116.84
8.3.130.0181.98316.84
8.3.120.0170.87716.84
8.3.110.0181.72016.84
8.3.100.0241.01916.84
8.3.90.0181.36216.84
8.3.80.0160.55516.84
8.3.70.0200.69716.84
8.3.60.0201.89916.84
8.3.50.0310.55216.84
8.3.40.0271.38417.86
8.3.30.0151.09117.86
8.3.20.0120.80017.64
8.3.10.0160.70417.92
8.3.00.0151.27117.57
8.2.280.0171.06116.84
8.2.270.0160.47816.84
8.2.260.0200.87716.84
8.2.250.0130.58416.84
8.2.240.0161.65116.84
8.2.230.0160.48916.84
8.2.220.0171.56416.84
8.2.210.0070.60616.84
8.2.200.0071.70618.31
8.2.190.0040.71418.45
8.2.180.0051.15518.41
8.2.170.0071.54419.52
8.2.160.0111.00919.34
8.2.150.0090.65219.36
8.2.140.0061.17119.47
8.2.130.0040.66719.38
8.2.120.0060.76319.40
8.2.110.0110.92619.43
8.2.100.0081.67919.55
8.2.90.0121.13317.89
8.2.80.0060.98119.43
8.2.70.0030.91419.42
8.2.60.0071.22919.39
8.2.50.0131.04119.54
8.2.40.0111.09117.76
8.2.30.0150.98117.89
8.2.20.0111.70217.81
8.2.10.0100.81519.59
8.2.00.0051.16719.43

preferences:
26.67 ms | 403 KiB | 5 Q