JustinK101 Posted May 23, 2011 Share Posted May 23, 2011 I have the following simple code to test against collision on a primary key I am creating: $machine_ids = array(); for($i = 0; $i < 100000; $i++) { //Generate machine id returns a 15 character alphanumeric string $mid = Functions::generate_machine_id(); if(in_array($mid, $machine_ids)) { die("Collision!"); } else { $machine_ids[] = $mid; } } die("Success!"); Any idea why this is taking minutes to run? Anyway to speed it up? Quote Link to comment https://forums.phpfreaks.com/topic/237190-in_array-very-slow-performance/ Share on other sites More sharing options...
gizmola Posted May 23, 2011 Share Posted May 23, 2011 Maybe it's because you are generating 100000 15 character strings (using some algorithm you didn't include) and checking that there are no duplicates. Quote Link to comment https://forums.phpfreaks.com/topic/237190-in_array-very-slow-performance/#findComment-1218955 Share on other sites More sharing options...
JustinK101 Posted May 23, 2011 Author Share Posted May 23, 2011 The generator algorithm inst the problem, ran the code without the check and under a second. I know I am checking 100,000. That's the entire point. Honestly, 100,000 element array inst that much in today's scale. Quote Link to comment https://forums.phpfreaks.com/topic/237190-in_array-very-slow-performance/#findComment-1218957 Share on other sites More sharing options...
gizmola Posted May 23, 2011 Share Posted May 23, 2011 Sorry I don't believe you. You ran your algorithm 100k times and it ran in 1 second? I'd like to see some proof of that. Quote Link to comment https://forums.phpfreaks.com/topic/237190-in_array-very-slow-performance/#findComment-1218959 Share on other sites More sharing options...
JustinK101 Posted May 23, 2011 Author Share Posted May 23, 2011 Stackoverflow to the rescue, here is the solution, runs in 2 seconds. for($i = 0; $i < 100000; $i++) { //Generate machine id returns a 15 character alphanumeric string $mid = Functions::generate_machine_id(); if (isset($machine_ids[$mid])) { die("Collision!"); } else { $machine_ids[$mid] = true; } } Quote Link to comment https://forums.phpfreaks.com/topic/237190-in_array-very-slow-performance/#findComment-1218960 Share on other sites More sharing options...
JustinK101 Posted May 23, 2011 Author Share Posted May 23, 2011 @gizmola Here is the function you wanted to see by the way. No wizardry, just fast. public static function generate_machine_id($length = 15) { $password = ""; $possible = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; $maxlength = strlen($possible); if ($length > $maxlength) { $length = $maxlength; } $i = 0; while ($i < $length) { $password .= substr($possible, mt_rand(0, $maxlength-1), 1); $i++; } return $password; } Quote Link to comment https://forums.phpfreaks.com/topic/237190-in_array-very-slow-performance/#findComment-1218968 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.