php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #81561 Optimize `count` calls in conditions
Submitted: 2021-10-28 12:48 UTC Modified: 2021-10-28 13:32 UTC
From: lars at moelleken dot org Assigned: cmb (profile)
Status: Closed Package: *General Issues
PHP Version: Irrelevant OS:
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: lars at moelleken dot org
New email:
PHP Version: OS:

 

 [2021-10-28 12:48 UTC] lars at moelleken dot org
Description:
------------
While optimising (reducing) some `count` calls in phpstan-src, I thought: "Why do php count the full array in simpel conditions? Or is it already optimized?"

Example: `if (count($x) === 1)` 





Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-10-28 13:32 UTC] cmb@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: cmb
 [2021-10-28 13:32 UTC] cmb@php.net
count() is O(1) since the number of elements is stored in the
HashTable (basically the same for strings wrt. strlen()).  So it
is already optimized.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Mar 14 15:01:30 2025 UTC