mirror of
https://github.com/fzaninotto/Faker.git
synced 2025-03-24 09:19:50 +01:00
optimize randomElements performance
This commit is contained in:
parent
cae91904b8
commit
6d7f9f4495
@ -137,10 +137,18 @@ class Base
|
||||
if (count($array) < $count) {
|
||||
throw new \LengthException("Cannot get $count elements, only " . count($array) . ' in array');
|
||||
}
|
||||
$keys = array_keys($array);
|
||||
$highKey = count($keys) - 1;
|
||||
$numElements = 0;
|
||||
|
||||
$elements = array();
|
||||
while (count($elements) < $count) {
|
||||
$key = static::randomKey($array);
|
||||
while ($numElements < $count) {
|
||||
$key = $keys[mt_rand(0, $highKey)];
|
||||
if (isset($elements[$key])) {
|
||||
continue;
|
||||
}
|
||||
$elements[$key] = $array[$key];
|
||||
$numElements++;
|
||||
}
|
||||
|
||||
return array_values($elements);
|
||||
|
Loading…
x
Reference in New Issue
Block a user