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
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
18 - 4 = ?
Subscribe to this entry?

 
 [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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 30 18:01:30 2024 UTC