Skip to content

Instantly share code, notes, and snippets.

@vertexvaar
Created December 18, 2020 10:16
Show Gist options
  • Save vertexvaar/283217d8469578454d86916e6509f9ce to your computer and use it in GitHub Desktop.
Save vertexvaar/283217d8469578454d86916e6509f9ce to your computer and use it in GitHub Desktop.
<?php
$functions = [
'array_flip & isset' => function (array $haystack, array $needles) {
$haystackFlip = array_flip($haystack);
foreach ($needles as $key => $needle) {
if (!isset($haystackFlip[$needle])) {
unset($needles[$key]);
}
}
return $needles;
},
'array_key_exists (prepared)' => function (array $haystack, array $needles) {
foreach ($needles as $key => $needle) {
if (!array_key_exists($needle, $haystack)) {
unset($needles[$key]);
}
}
return $needles;
},
// 'array_flip & array_key_exists' => function (array $haystack, array $needles) {
// $haystackFlip = array_flip($haystack);
// foreach ($needles as $key => $needle) {
// if (!array_key_exists($needle, $haystackFlip)) {
// unset($needles[$key]);
// }
// }
// return $needles;
// },
'in_array' => function (array $haystack, array $needles) {
foreach ($needles as $key => $needle) {
if (!in_array($needle, $haystack)) {
unset($needles[$key]);
}
}
return $needles;
},
// 'in_array & array_unique' => function (array $haystack, array $needles) {
// $haystack = array_unique($haystack);
// foreach ($needles as $key => $needle) {
// if (!in_array($needle, $haystack)) {
// unset($needles[$key]);
// }
// }
// return $needles;
// },
];
$needles = [
'foo',
'bar',
'baz',
'beng',
'floo',
];
function generateRandomString($length = 10)
{
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, 61)];
}
return $randomString;
}
function arrayFactory(int $num, array $mustInclude = []): array
{
if ($num < count($mustInclude)) {
$mustIncludeRest = array_splice($mustInclude, 0, $num);
$mustInclude = $mustIncludeRest;
}
if ($num > count($mustInclude)) {
for ($i = $num - count($mustInclude); $i > 0; $i--) {
$mustInclude[] = generateRandomString(rand(20, 100));
}
}
shuffle($mustInclude);
return $mustInclude;
}
$canaries = [];
for ($j = 100000; $j >= 1; $j /= 10) {
$canaries[$j . ' no match'] = arrayFactory($j);
$canaries[$j . ' matches'] = arrayFactory($j, $needles);
}
$canaries['0'] = [];
$durations = [];
$results = [];
foreach ($functions as $functionLabel => $function) {
foreach ($canaries as $canaryLabel => $canary) {
if ($functionLabel === 'array_key_exists (prepared)') {
$canary = array_flip($canary);
}
$results[$canaryLabel][$functionLabel] = $function($canary, $needles);
$start = microtime(true);
for ($k = 0; $k < 1000; $k++) {
$function($canary, $needles);
}
$end = microtime(true);
$duration = $end - $start;
$durations[$canaryLabel][$functionLabel] = $duration;
}
}
foreach ($durations as &$functionDurations) {
arsort($functionDurations);
}
foreach ($results as $canLabel => $fnRes) {
$curr = null;
foreach ($fnRes as $func => $res) {
if (null === $curr) {
$curr = $res;
} elseif ($curr !== $res) {
throw new Exception('Wer Mist misst, misst Mist');
}
}
}
var_export($durations);
@vertexvaar
Copy link
Author

Results:

<?php
[
    '100000 no match' => [
        'array_flip & isset' => 8.823206901550293,
        'in_array' => 4.052308082580566,
        'array_key_exists (prepared)' => 0.004848003387451172,
    ],
    '100000 matches' => [
        'array_flip & isset' => 8.786531925201416,
        'in_array' => 1.4839370250701904,
        'array_key_exists (prepared)' => 0.004310131072998047,
    ],
    '10000 no match' => [
        'array_flip & isset' => 0.255079984664917,
        'in_array' => 0.11234402656555176,
        'array_key_exists (prepared)' => 0.004889011383056641,
    ],
    '10000 matches' => [
        'array_flip & isset' => 0.2567770481109619,
        'in_array' => 0.07295012474060059,
        'array_key_exists (prepared)' => 0.004347085952758789,
    ],
    '1000 no match' => [
        'array_flip & isset' => 0.018695831298828125,
        'in_array' => 0.015958070755004883,
        'array_key_exists (prepared)' => 0.004851818084716797,
    ],
    '1000 matches' => [
        'array_flip & isset' => 0.01786518096923828,
        'in_array' => 0.009994029998779297,
        'array_key_exists (prepared)' => 0.004354953765869141,
    ],
    '100 no match' => [
        'in_array' => 0.008791923522949219,
        'array_key_exists (prepared)' => 0.004847049713134766,
        'array_flip & isset' => 0.0047550201416015625,
    ],
    '100 matches' => [
        'in_array' => 0.0062220096588134766,
        'array_key_exists (prepared)' => 0.004394054412841797,
        'array_flip & isset' => 0.004287004470825195,
    ],
    '10 no match' => [
        'in_array' => 0.004911184310913086,
        'array_key_exists (prepared)' => 0.0048677921295166016,
        'array_flip & isset' => 0.003576040267944336,
    ],
    '10 matches' => [
        'in_array' => 0.004410982131958008,
        'array_key_exists (prepared)' => 0.004339933395385742,
        'array_flip & isset' => 0.0031058788299560547,
    ],
    '1 no match' => [
        'in_array' => 0.004850864410400391,
        'array_key_exists (prepared)' => 0.004825115203857422,
        'array_flip & isset' => 0.003459930419921875,
    ],
    '1 matches' => [
        'in_array' => 0.005812883377075195,
        'array_key_exists (prepared)' => 0.004786968231201172,
        'array_flip & isset' => 0.0034101009368896484,
    ],
    0 => [
        'in_array' => 0.007040977478027344,
        'array_key_exists (prepared)' => 0.004853010177612305,
        'array_flip & isset' => 0.0034339427947998047,
    ],
];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment