php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69440 Perfomance of function count() with array getted by references
Submitted: 2015-04-13 22:39 UTC Modified: 2015-04-14 07:00 UTC
From: mikheev at 179 dot ru Assigned: nikic (profile)
Status: Closed Package: Performance problem
PHP Version: 5.6.8RC1 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: mikheev at 179 dot ru
New email:
PHP Version: OS:

 

 [2015-04-13 22:39 UTC] mikheev at 179 dot ru
Description:
------------
If I execute count($arr) from function foo(&$arr), it will have a complexity O(n) (feels like) instead of the expected O(1), where `n` is count of elements in array.

Is it correct? Why?

Test script:
---------------
http://sandbox.onlinephpfunctions.com/code/f2d23edf84bbfe841d3bffda8373af8d07e51a19

Expected result:
----------------
FooWithArrayCopy: 0.00056791305541992
FooWithArrayCopy: 0.00046791305541992

Actual result:
--------------
FooWithArrayCopy: 0.00056791305541992
FooWithReference: 2.7791709899902

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-04-14 07:00 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2015-04-14 07:00 UTC] nikic@php.net
Because in PHP 5 an array cannot be shared between a reference ($arr) and a value (the count() argument).

This has been fixed in PHP 7, where this will be possible without copying the array.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 02 02:01:38 2025 UTC