note 22360 added to function.array-rand
| From: | josh at 3io dot com | Date: | Fri, 14 Jun 2002 19:20:58 +0000 |
| Subject: | note 22360 added to function.array-rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-31870@lists.php.net to get a copy of this message | ||
I modified fake_array_rand to always only return 1 element, and did some benchmarks against calling
array_rand with the second parameter as 1. I ran 100 samples for each function for each number of
elements and took the average result. While the internal array_rand is faster for a small number of
elements, it scales very poorly.
1 elements: 2.0619630813599E-05 sec. for array_rand,8.4352493286133E-05 sec. for fake_array_rand
10 elements: 2.1675825119019E-05 sec. for array_rand,8.427619934082E-05 sec. for fake_array_rand
100 elements: 2.9319524765015E-05 sec. for array_rand,8.4599256515503E-05 sec. for fake_array_rand
1000 elements: 0.0001157283782959 sec. for array_rand,8.5572004318237E-05 sec. for fake_array_rand
10000 elements: 0.0016669762134552 sec. for array_rand,8.5201263427734E-05 sec. for fake_array_rand
100000 elements: 0.015599734783173 sec. for array_rand,8.5580348968506E-05 sec. for fake_array_rand
1000000 elements: 0.18011983394623 sec. for array_rand,8.6690187454224E-05 sec. for fake_array_rand
function fake_array_rand ($array)
{
$count = count ($array);
# Help keep the number generator random :)
$randval and usleep ("0.$randval");
# Seed the random number generator
# Generate a random number
srand ((double) microtime() * 10000000);
$randval = rand();
# Use the random value to 'pick' an entry from the array
# Count the number of times that the entry is picked
++$index[$randval % $count];
return $array[$randval % $count];
}
--
http://www.php.net/manual/en/function.array-rand.php
http://master.php.net/manage/user-notes.php?action=edit+22360
http://master.php.net/manage/user-notes.php?action=delete+22360
http://master.php.net/manage/user-notes.php?action=reject+22360