3v4l.org

run code in 300+ PHP versions simultaneously
<?php function noSort($a, $b) { $result = []; foreach ($a as $v) { $index = array_search($v, $b); if ($index !== false) { $result[] = $v; unset($b[$index]); } } return $result; } function intersectSimpler(...$twoArrays) { $result = []; sort($twoArrays); foreach ($twoArrays[0] as $v) { $index = array_search($v, $twoArrays[1]); if ($index !== false) { $result[] = $v; unset($twoArrays[1][$index]); } } return $result; } function intersectSimplest($a, $b): array { $result = []; if (count($a) < count($b)) { $short = $a; $long = $b; } else { $short = $b; $long = $a; } foreach ($short as $v) { $index = array_search($v, $long); if ($index !== false) { $result[] = $v; unset($long[$index]); } } return $result; } function intersectSimplish($a, $b): array { $result = []; if (count($a) < count($b)) { $short = $a; $long = $b; } else { $short = $b; $long = $a; } foreach ($short as $v) { if (in_array($v, $long)) { $result[] = $v; unset($long[array_search($v, $long)]); } } return $result; } function intersectSimple($a, $b) { $result = array(); $short = count($a) < count($b) ? $a : $b; $long = count($a) < count($b) ? $b : $a; foreach ($short as $v) { if (in_array($v, $long)) { //if found add to results and remove from b $result[] = $v; unset($long[array_search($v, $long)]); } } return $result; } function intersectAderrahim($a, $b) { $a_values_count = array_count_values($a); $b_values_count = array_count_values($b); $res = array_values(array_intersect($a, $b)); $res_values_count = array_count_values($res); foreach ($res as $key => $val) { if ($res_values_count[$val] > $a_values_count[$val] || $res_values_count[$val] > $b_values_count[$val]) { unset($res[$key]); $res_values_count[$val]--; } } return array_values($res); } //Start timer $start = microtime(true); echo "Start Test\n"; //Run code 100000 times for ($i = 0; $i < 100000; $i++) { $a = [1, 1, 1, 1, 2, 3, 4, 4, 5, 6, 8, 8, 8]; $b = [1, 1, 3, 3, 5, 5, 5, 6, 7, 9, 9]; $result = intersectAderrahim($a, $b); } //Stop timer $end = microtime(true); $time = $end - $start; //Print performance in microseconds echo "Performance Aderrahim: $time\n"; //Start timer $start = microtime(true); echo "Start Test\n"; //Run code 100000 times for ($i = 0; $i < 100000; $i++) { $a = [1, 1, 1, 1, 2, 3, 4, 4, 5, 6, 8, 8, 8]; $b = [1, 1, 3, 3, 5, 5, 5, 6, 7, 9, 9]; $result = noSort($a, $b); } //Stop timer $end = microtime(true); $time = $end - $start; //Print performance in microseconds echo "Performance NoSort: $time\n"; //Start timer $start = microtime(true); echo "Start Test\n"; //Run code 100000 times for ($i = 0; $i < 100000; $i++) { $a = [1, 1, 1, 1, 2, 3, 4, 4, 5, 6, 8, 8, 8]; $b = [1, 1, 3, 3, 5, 5, 5, 6, 7, 9, 9]; $result = intersectSimpler($a, $b); } //Stop timer $end = microtime(true); $time = $end - $start; //Print performance in microseconds echo "Performance Simpler: $time\n"; //Start timer $start = microtime(true); echo "Start Test\n"; //Run code 100000 times for ($i = 0; $i < 100000; $i++) { $a = [1, 1, 1, 1, 2, 3, 4, 4, 5, 6, 8, 8, 8]; $b = [1, 1, 3, 3, 5, 5, 5, 6, 7, 9, 9]; $result = intersectSimplish($a, $b); } //Stop timer $end = microtime(true); $time = $end - $start; //Print performance in microseconds echo "Performance Simplish: $time\n"; //Start timer $start = microtime(true); echo "Start Test\n"; //Run code 100000 times for ($i = 0; $i < 100000; $i++) { $a = [1, 1, 1, 1, 2, 3, 4, 4, 5, 6, 8, 8, 8]; $b = [1, 1, 3, 3, 5, 5, 5, 6, 7, 9, 9]; $result = intersectSimplest($a, $b); } //Stop timer $end = microtime(true); $time = $end - $start; //Print performance in microseconds echo "Performance Simplest: $time\n"; //Start timer $start = microtime(true); echo "Start Test\n"; //Run code 100000 times for ($i = 0; $i < 100000; $i++) { $a = [1, 1, 1, 1, 2, 3, 4, 4, 5, 6, 8, 8, 8]; $b = [1, 1, 3, 3, 5, 5, 5, 6, 7, 9, 9]; $result = intersectSimple($a, $b); } //Stop timer $end = microtime(true); $time = $end - $start; //Print performance in microseconds echo "Performance Simple: $time\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.2.00.0030.51717.86
8.1.130.0030.53717.43
8.1.120.0030.54217.66
8.1.110.0000.55017.57
8.1.100.0070.54617.48
8.1.90.0000.55317.48
8.1.80.0000.52719.32
8.1.70.0070.53217.55
8.1.60.0030.54219.30
8.1.50.0030.55117.54
8.1.40.0030.55717.72
8.1.30.0030.53217.77
8.1.20.0100.52117.67
8.1.10.0030.55319.35
8.1.00.0070.55117.61
8.0.260.0070.53718.51
8.0.250.0030.54918.71
8.0.240.0070.55117.41
8.0.230.0030.56417.41
8.0.220.0000.54717.41
8.0.210.0070.56418.64
8.0.200.0000.55318.63
8.0.190.0030.55418.66
8.0.180.0070.55918.54
8.0.170.0030.55318.54
8.0.160.0030.54517.41
8.0.150.0030.57817.41
8.0.140.0100.58018.55
8.0.130.0000.56217.41
8.0.120.0030.54517.41
8.0.110.0000.56217.41
8.0.100.0070.56717.41
8.0.90.0070.53817.41
8.0.80.0030.53717.41
8.0.70.0070.56017.41
8.0.60.0030.54117.41
8.0.50.0030.55017.41
8.0.30.0000.55817.41
8.0.20.0000.56117.41
8.0.10.0000.54917.41

preferences:
44.84 ms | 403 KiB | 5 Q