Created
December 18, 2020 10:16
-
-
Save vertexvaar/283217d8469578454d86916e6509f9ce to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Results: