1
0
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:
David Stensland 2014-03-05 12:55:08 -05:00
parent cae91904b8
commit 6d7f9f4495

View File

@ -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);