Req #79421 [Com]: foreach (array_keys($arr) as $k) is slow

From: Date: Fri, 10 Apr 2020 07:00:55 +0000
Subject: Req #79421 [Com]: foreach (array_keys($arr) as $k) is slow
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-226514@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=79421&edit=1

 ID:                 79421
 Comment by:         alexinbeijing at gmail dot com
 Reported by:        michael dot vorisek at email dot cz
 Summary:            foreach (array_keys($arr) as $k) is slow
 Status:             Open
 Type:               Feature/Change Request
 Package:            Performance problem
 PHP Version:        7.4.4
 Block user comment: N
 Private report:     N

 New Comment:

Interestingly, benchmarking shows that the 2nd form of the code you show here is actually *slower*
than the 1st.

I don't know if foreach(array_keys(...) as ...) has already been optimized internally, but in
any case, it doesn't seem that this suggested optimization is necessary. Recommend this ticket
can be closed.


Previous Comments:
------------------------------------------------------------------------
[2020-03-27 19:50:24] michael dot vorisek at email dot cz

Description:
------------
This is a feature request to optimize:

foreach (array_keys($arr) as $k) {}

instead of building array of keys optimize this in PHP internally to behave like:

foreach ($arr as $k => $ignore) {}

It is important to optimize it as foreach on keys is very commonly used.



------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=79421&edit=1


Thread (4 messages)

« previous php.bugs (#226514) next »