Skip to content

Instantly share code, notes, and snippets.

@SimoTod
Last active January 15, 2020 22:23
Show Gist options
  • Save SimoTod/e91b09f8b003f19b324d6b120954a485 to your computer and use it in GitHub Desktop.
Save SimoTod/e91b09f8b003f19b324d6b120954a485 to your computer and use it in GitHub Desktop.
<?php
//https://codepen.io/SimoTod/pen/RwNYNXm
$str_needle = 's6tbdfgj222dJGk';
$str_seed = str_repeat("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", 10);
foreach ([10000, 1000, 100, 10] as $int_number_of_item) {
echo "N={$int_number_of_item}\n";
//Populate array
$arr_heystack = [];
for ($int_counter = 0; $int_counter < $int_number_of_item; $int_counter++) {
$arr_heystack[] = substr(str_shuffle($str_seed), 0, rand(10, 100));;
}
//benchmarks
$int_time = microtime(1);
$mix_test = in_array($str_needle, $arr_heystack);
echo "in_array: ", number_format(microtime(1) - $int_time, 10), PHP_EOL;
$int_time = microtime(1);
$mix_test = array_search($str_needle, $arr_heystack);
echo "array_search: ", number_format(microtime(1) - $int_time, 10), PHP_EOL;
$int_time = microtime(1);
$arr_heystack = array_flip($arr_heystack);
$mix_test = isset($arr_heystack[$str_needle]);
echo "flip+isset: ", number_format(microtime(1) - $int_time, 10), PHP_EOL;
echo PHP_EOL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment