Re: [PHP-DEV] array_filter vs for/foreach usage cost

This is only part of a thread. view whole thread
  100472
September 8, 2017 15:06 johannes@schlueters.de (Johannes =?ISO-8859-1?Q?Schl=FCter?=)
Hi,

On Do, 2017-09-07 at 15:13 -0300, David Rodrigues wrote:
> I understand that array_filter() should costs more than for/foreach > because > it is a function call that call another function for each item, while > the > for/foreach is a language constructor that works on a way totally > different > and its optimized for this kind of job.
The main cost in that is that array_filter has to create a copy of the array. An alternative is using an FilterIterator or generator or such which would make this a pipeline step. (with iterators there are multiple function calls, while the function descriptor is cached, making them relatively cheap) I'd focus on improving those. johannes
  100473
September 8, 2017 15:35 levim@php.net (Levi Morrison)
On Fri, Sep 8, 2017 at 9:06 AM, Johannes Schlüter
<johannes@schlueters.de> wrote:
> Hi, > > On Do, 2017-09-07 at 15:13 -0300, David Rodrigues wrote: >> I understand that array_filter() should costs more than for/foreach >> because >> it is a function call that call another function for each item, while >> the >> for/foreach is a language constructor that works on a way totally >> different >> and its optimized for this kind of job. > > The main cost in that is that array_filter has to create a copy of the > array. An alternative is using an FilterIterator or generator or such > which would make this a pipeline step. (with iterators there are > multiple function calls, while the function descriptor is cached, > making them relatively cheap) I'd focus on improving those.
As far as I can tell from the source `array_filter` does not create a copy of the array. It initializes a new array and then appends to it. If we want to improve the performance of `array_filter` we need to examine output of common compilers to see if they hoist the have_callback check out of the loop; I suspect it may not given the complexity of ZEND_HASH_FOREACH_KEY_VAL_IND.
  100474
September 8, 2017 18:17 pollita@php.net (Sara Golemon)
On Fri, Sep 8, 2017 at 8:06 AM, Johannes Schlüter
<johannes@schlueters.de> wrote:
> On Do, 2017-09-07 at 15:13 -0300, David Rodrigues wrote: >> I understand that array_filter() should costs more than for/foreach >> because >> it is a function call that call another function for each item, while >> the >> for/foreach is a language constructor that works on a way totally >> different >> and its optimized for this kind of job. > > The main cost in that is that array_filter has to create a copy of the > array. An alternative is using an FilterIterator or generator or such > which would make this a pipeline step. (with iterators there are > multiple function calls, while the function descriptor is cached, > making them relatively cheap) I'd focus on improving those. > *cough*
https://github.com/phplang/generator/blob/master/src/iterable.php#L64 *cough* -Sara