PHP quickly gets random small arrays in large arrays
There is a function to guess what users like. Users can refresh the recommended content by clicking to change a group.
First query the database to get the first 1000 pieces of data, and then randomly remove 4 pieces.
After I finished writing the program, I found that it ran very slowly. Every time I refreshed it, I had to wait a long time for it to come out.
The code is as follows:
$arr = range(1, 100000); $start = time(); for($i = 0; $i < 100; $i ++){ $key = mt_rand(0, 99999 - $i); $result[] = $arr[$key]; unset($arr[$key]); sort($arr); } $end = time(); echo $end - $start;
The above writing method must be sorted after using unset.
unset will only delete the key value, but the key will be retained.
The improved writing method is as follows:
$arr = range(1, 100000); $start = time(); for($i = 0; $i < 100; $i ++){ $key = mt_rand(0, 99999 - $i); $result[] = array_splice($arr, $key, 1); } $end = time(); echo $end - $start;
The efficiency has increased by 3-4 times.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

