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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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

Add a Patch

Pull Requests

Add a Pull Request

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