Skip to content

Performace problems using count() #19

@dimitrijjedich

Description

@dimitrijjedich

As the initial implementation was just to collect algorithms for benchmarking, the initial implementations seem to almost all have a flaw that was learned only in the later development stages and never refactored:

When iterating over an arry the initial approach was this:

for ($i = 0; $i < count($arr); $i++) {

instead of the right way:

$length = count($arr)
for ($i = 0; $i < $length; $i++) {

The first implementation has a flaw that may or may not be correct but it seems depending on the PHP environment (or even all of them) the count will be calculated on every run of the loop. Depending on the correctness of this and assuming an array with the sice of 1000 elements, the first approach will calculate the length of the array 999 more times the the second one.

Metadata

Metadata

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions