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); }
Output for git.master_jit
=== Array Intersection Benchmark for 20 elements === ------------------------------------------------------------ Method | Time (ms) | Memory ------------------------------------------------------------ array_intersect() | 0.023 ms | 3.02 KB manual_intersect() | 0.004 ms | 1.98 KB flipped_intersect() | 0.002 ms | 3.97 KB ------------------------------------------------------------ === Array Intersection Benchmark for 20,000 elements === ------------------------------------------------------------ Method | Time (ms) | Memory ------------------------------------------------------------ array_intersect() | 29.733 ms | 1.88 MB manual_intersect() | 0.950 ms | 1.75 MB flipped_intersect() | 1.146 ms | 3.00 MB ------------------------------------------------------------ === Array Intersection Benchmark for 200,000 elements === Fatal error: Out of memory (allocated 31469568 bytes) (tried to allocate 4194312 bytes) in /in/2kHVL on line 73 Stack trace: #0 /in/2kHVL(73): array_unique(Array) #1 /in/2kHVL(112): runBenchmarks(200000) #2 {main} mmap() failed: [12] Cannot allocate memory mmap() failed: [12] Cannot allocate memory
Process exited with code 255.
Output for git.master
=== Array Intersection Benchmark for 20 elements === ------------------------------------------------------------ Method | Time (ms) | Memory ------------------------------------------------------------ array_intersect() | 0.025 ms | 3.02 KB manual_intersect() | 0.004 ms | 1.98 KB flipped_intersect() | 0.002 ms | 3.97 KB ------------------------------------------------------------ === Array Intersection Benchmark for 20,000 elements === ------------------------------------------------------------ Method | Time (ms) | Memory ------------------------------------------------------------ array_intersect() | 28.513 ms | 1.88 MB manual_intersect() | 0.946 ms | 1.75 MB flipped_intersect() | 1.060 ms | 2.55 MB ------------------------------------------------------------ === Array Intersection Benchmark for 200,000 elements === Fatal error: Out of memory (allocated 37756928 bytes) (tried to allocate 2097160 bytes) in /in/2kHVL on line 74 Stack trace: #0 /in/2kHVL(74): array_unique(Array) #1 /in/2kHVL(112): runBenchmarks(200000) #2 {main} mmap() failed: [12] Cannot allocate memory mmap() failed: [12] Cannot allocate memory mmap() failed: [12] Cannot allocate memory
Process exited with code 255.

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
39.42 ms | 781 KiB | 4 Q