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";
Output for git.master
Start Test Performance Aderrahim: 0.17467594146729 Start Test Performance NoSort: 0.063039064407349 Start Test Performance Simpler: 0.083665132522583 Start Test Performance Simplish: 0.069833993911743 Start Test Performance Simplest: 0.059314966201782 Start Test Performance Simple: 0.069069147109985
Output for git.master_jit
Start Test Performance Aderrahim: 0.17832612991333 Start Test Performance NoSort: 0.062835216522217 Start Test Performance Simpler: 0.083383083343506 Start Test Performance Simplish: 0.069926023483276 Start Test Performance Simplest: 0.059938907623291 Start Test Performance Simple: 0.070556163787842

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:
49.84 ms | 407 KiB | 5 Q