[php-src] Issue #8283: Performance: array_reduce is drastically slower (~1000x) than implementing the same logic with foreach
| From: | iluuu1994 | Date: | Wed, 30 Mar 2022 22:08:06 +0000 |
| Subject: | [php-src] Issue #8283: Performance: array_reduce is drastically slower (~1000x) than implementing the same logic with foreach | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-240585@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/8283
Comment Author: iluuu1994
Hi @Firehed!
The problem is that you're causing array separation (arrays are copy on write, unless their
reference count is 1) when doing
@$carry[$item]++;. The array is being copied over and
over again for every iteration of array_reduce. The same would happen for code like
this:
```php
$result = [];
foreach ($data as $item) {
$dummy = $result;
@$result[$item]++;
}
```
That code is pretty much equivalent in performance as the array_reduce example. So
I'm closing this as not a bug.