There is a guess user like the function, the user can click to change a group, to refresh the recommended content.
First through the database query, get the first 1000 data, and then randomly remove 4.
After the program has been written, found that the operation is very slow, each refresh, it will take a long time to come out.
The code is as follows:
$arr = Range (1, 100000), $start = time (), for ($i = 0; $i <, $i + +) {$key = Mt_rand (0, 99999-$i); $result [] = $arr [$ke Y];unset ($arr [$key]); sort ($arr);} $end = time (), Echo $end-$start;
The above notation, after using unset, must be sorted.
Unset only deletes the key value, and key is preserved.
After the improvement, the following wording:
$arr = Range (1, 100000), $start = time (), for ($i = 0; $i <, $i + +) {$key = Mt_rand (0, 99999-$i); $result [] = array_s Plice ($arr, $key, 1);} $end = time (), Echo $end-$start;
3-4 times more efficient.
PHP quickly get random decimal groups in large arrays