Skip to content

Instantly share code, notes, and snippets.

@silverslice
Last active September 22, 2015 23:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save silverslice/70a4476e3fec4248f7e6 to your computer and use it in GitHub Desktop.
Save silverslice/70a4476e3fec4248f7e6 to your computer and use it in GitHub Desktop.
Test arrays comparasion
<?php
// test variants
$array_first = ["a" => "green", "b" => "brown", "c" => "blue", "red", "array", "array_search", "test"];
$array_second = ["a" => "green", "b" => "yellow", "red", "array_search", "blue", "benchmark"];
test('compare1', $array_first, $array_second);
test('compare2', $array_first, $array_second);
test('compare3', $array_first, $array_second);
test('compare4', $array_first, $array_second);
// generate random values
$count = 100;
$array_first_values = [];
$array_second_values = [];
for ($n = 0; $n < $count; $n++) {
for ($i = 0; $i < 1000; $i++) {
$array_first_values[$n][$i] = str_shuffle('abcdefg'); // 7! = 5040
}
for ($i = 0; $i < 1000; $i++) {
$array_second_values[$n][$i] = str_shuffle('abcdefg');
}
}
run('compare1', $array_first_values, $array_second_values);
run('compare2', $array_first_values, $array_second_values);
run('compare3', $array_first_values, $array_second_values);
run('compare4', $array_first_values, $array_second_values);
function test($func, $array_first, $array_second)
{
$expected = [
'green' => ['a', 'a'],
'blue' => ['c', 2],
'red' => [0, 0],
'array_search' => [2, 1],
];
$res = $func($array_first, $array_second);
echo $func . ': ';
if ($res === $expected) {
echo 'pass';
} else {
echo 'ERROR';
}
echo PHP_EOL;
}
function run($func, $array_first_values, $array_second_values)
{
$count = count($array_first_values);
$start_time = microtime(true);
for ($i = 0; $i < $count; $i++) {
$func($array_first_values[$i], $array_second_values[$i]);
}
echo $func . ': ' . (microtime(true) - $start_time) . 's' . PHP_EOL;
}
function compare1($array_first, $array_second)
{
$res = [];
foreach ($array_first as $key => $value) {
if (in_array($value, $array_second, true)) {
$key_second = array_search($value, $array_second, true);
$res[$value] = [$key, $key_second];
}
}
return $res;
}
function compare2($array_first, $array_second)
{
$res = [];
foreach ($array_first as $key => $value) {
$key_second = array_search($value, $array_second, true);
if ($key_second !== false) {
$res[$value] = [$key, $key_second];
}
}
return $res;
}
function compare3($array_first, $array_second)
{
$res = [];
$array_second_rev = array_flip($array_second);
foreach ($array_first as $key => $value) {
if (isset($array_second_rev[$value])) {
$res[$value] = [$key, $array_second_rev[$value]];
}
}
return $res;
}
function compare4($array_first, $array_second)
{
$res = [];
$ar1 = array_intersect($array_first, $array_second);
$ar2 = array_intersect($array_second, $array_first);
foreach ($ar1 as $key => $value) {
$key_second = array_search($value, $ar2, true);
if ($key_second !== false) {
$res[$value] = [$key, $key_second];
}
}
return $res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment