3v4l.org

run code in 500+ PHP versions simultaneously
<?php // Source - https://stackoverflow.com/a/9276284 // Posted by kingmaple, modified by community. See post 'Timeline' for change history // Retrieved 2026-04-08, License - CC BY-SA 4.0 // Source - https://stackoverflow.com/a/53203232 // Posted by slaszu, modified by community. See post 'Timeline' for change history // Retrieved 2026-04-08, License - CC BY-SA 4.0 // ini_set('memory_limit', '2048M'); function formatBytes(int $bytes): string { $units = ['B', 'KB', 'MB', 'GB']; $i = 0; while ($bytes >= 1024 && $i < count($units) - 1) { $bytes /= 1024; $i++; } return sprintf("%.2f %s", $bytes, $units[$i]); } function benchmark(callable $fn, string $label): array { gc_collect_cycles(); gc_mem_caches(); memory_reset_peak_usage(); $mem = -memory_get_peak_usage(); $time = -hrtime(true); $fn(); $time += hrtime(true); $mem += memory_get_peak_usage(); return [ 'label' => $label, 'time_ms' => $time / 1e6, 'mem_used' => $mem, ]; } function manual_intersect($arrayOne, $arrayTwo) { $index = array_flip($arrayOne); foreach ($arrayTwo as $value) { if (isset($index[$value])) { unset($index[$value]); } } foreach ($index as $value => $key) { unset($arrayOne[$key]); } return $arrayOne; } function flipped_intersect($arrayOne, $arrayTwo) { $index = array_flip($arrayOne); $second = array_flip($arrayTwo); $x = array_intersect_key($index, $second); return array_flip($x); } function runBenchmarks(int $n): void { echo "\n=== Array Intersection Benchmark for " . number_format($n) . " elements ===\n"; // Generate test arrays $one = []; $two = []; for ($i = 0; $i < $n; $i++) { $one[] = rand(0, 1000000); $two[] = rand(0, 100000); $two[] = rand(0, 10000); } $one = array_unique($one); $two = array_unique($two); $results = []; $results[] = benchmark( fn() => $res = array_intersect($one, $two), 'array_intersect()' ); $results[] = benchmark( fn() => $res = manual_intersect($one, $two), 'manual_intersect()' ); $results[] = benchmark( fn() => $res = flipped_intersect($one, $two), 'flipped_intersect()' ); // --- Print Table --- echo str_repeat('-', 60) . "\n"; printf("%-25s | %-14s | %-15s\n", 'Method', 'Time (ms)', 'Memory'); echo str_repeat('-', 60) . "\n"; foreach ($results as $r) { printf("%-25s | %11.3f ms | %15s\n", $r['label'], $r['time_ms'], formatBytes($r['mem_used']) ); } echo str_repeat('-', 60) . "\n"; } // Run for various sizes foreach ([20, 20000, 200000] as $n) { runBenchmarks($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.5.30.0800.29743.90
8.5.20.0660.29543.87
8.5.10.0580.30343.97
8.5.00.0570.29343.68
8.4.180.0350.08545.24
8.4.170.0390.08445.17
8.4.160.0400.09544.98
8.4.150.0260.07445.31
8.4.140.0510.30445.05
8.4.130.0450.30345.12
8.4.120.0460.31045.16
8.4.110.0690.32045.14
8.4.100.0570.30144.96
8.4.90.0510.30545.20
8.4.80.0610.30845.18
8.4.70.0540.32344.98
8.4.60.0490.30245.16
8.4.50.0620.31345.23
8.4.40.0540.29645.02
8.4.30.0610.30744.91
8.4.20.0650.30145.01
8.4.10.0540.30645.07
8.3.300.0400.07143.57
8.3.290.0310.07543.49
8.3.280.0450.07043.27
8.3.270.0610.29944.03
8.3.260.0540.30444.13
8.3.250.0580.29643.89
8.3.240.0490.29843.93
8.3.230.0700.30644.43
8.3.220.0700.30444.21
8.3.210.0530.29344.07
8.3.200.0720.28243.95
8.3.190.0610.30743.77
8.3.180.0570.29444.03
8.3.170.0540.28743.96
8.3.160.0530.30244.61
8.3.150.0450.30344.09
8.3.140.0480.30144.09
8.3.130.0460.30744.04
8.3.120.0600.32444.00
8.3.110.0540.30644.05
8.3.100.0560.29744.11
8.3.90.0650.30143.87
8.3.80.0720.31244.01
8.3.70.0570.29144.11
8.3.60.0470.31444.57
8.3.50.0670.29344.27
8.3.40.0290.05334.03
8.3.30.0350.07433.96
8.3.20.0230.05834.12
8.3.10.0410.06933.84
8.3.00.0240.05834.11

preferences:
45.73 ms | 779 KiB | 5 Q