note 22360 modified in function.array-rand by vrana
| From: | vrana@php.net | Date: | Tue, 17 Aug 2004 14:48:49 +0000 |
| Subject: | note 22360 modified in function.array-rand by vrana | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-80080@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
<?php
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];
}
?>
--was--
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://php.net/manual/en/function.array-rand.php