php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #67346 array_rand() performance issue
Submitted: 2014-05-27 11:40 UTC Modified: 2017-03-11 18:15 UTC
From: v dot martyanov at drweb dot com Assigned: nikic (profile)
Status: Closed Package: Arrays related
PHP Version: 5.5.12 OS: Any
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: v dot martyanov at drweb dot com
New email:
PHP Version: OS:

 

 [2014-05-27 11:40 UTC] v dot martyanov at drweb dot com
Description:
------------
There is a performance issue in array_rand(). The problem is in using doubles instead of long integers in the source.

In the case when num_req == 1 and num_avail < 256 it's not so hard to use tables with precomputed values and avoid the usage of divisions and doubles.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-03-11 18:15 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2017-03-11 18:15 UTC] nikic@php.net
PHP 7.1 uses a significantly faster array_rand() algorithm, especially for the num_req==1 case. It is now O(1) instead of O(n).
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 09 11:01:34 2025 UTC