Created
January 7, 2012 18:27
-
-
Save jonasb/1575566 to your computer and use it in GitHub Desktop.
Hash collision benchmark
This file contains 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
#!/usr/bin/env php | |
<?php | |
// | |
// Benchmark of adding items to associative array with hash collisions. | |
// | |
// generate all permutations of $substrings with $count number of concatenations | |
function do_generate_strings(array $substrings, array &$strings, $prefix, $count) { | |
if ($count == 1) { | |
foreach ($substrings as $s) { | |
$strings[] = "$prefix$s"; | |
} | |
return; | |
} | |
foreach ($substrings as $s) { | |
do_generate_strings($substrings, $strings, "$prefix$s", $count - 1); | |
} | |
} | |
function generate_strings($count) { | |
$strings = array(); | |
$colliding_substrings = array('ar', 'bQ', 'c0'); | |
do_generate_strings($colliding_substrings, $strings, '', $count); | |
return $strings; | |
} | |
$strings = generate_strings(10); | |
echo "n, duriation (s), memory\n"; | |
for ($n = 0; $n <= count($strings); $n += 1000) { | |
$mem_start = memory_get_usage(); | |
$time_start = microtime(true); | |
$params = array(); | |
for ($i = 0; $i < $n; $i++) { | |
$params[$strings[$i]] = ''; | |
} | |
$time_end = microtime(true); | |
$mem_end = memory_get_usage(); | |
$time = $time_end - $time_start; | |
$mem = $mem_end - $mem_start; | |
echo "$n, $time, $mem\n"; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment