Re: array_filter vs for/foreach usage cost
| From: | Johannes Schlüter | Date: | Fri, 08 Sep 2017 15:06:29 +0000 |
| Subject: | Re: array_filter vs for/foreach usage cost | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-100472@lists.php.net to get a copy of this message | ||
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