Bug #73571 [NEW]: foreach() way slower than while(list($key,$value)=each($arr))
| From: | spam2 at rhsoft dot net | Date: | Mon, 21 Nov 2016 08:56:56 +0000 |
| Subject: | Bug #73571 [NEW]: foreach() way slower than while(list($key,$value)=each($arr)) | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-205502@lists.php.net to get a copy of this message | ||
From: spam2 at rhsoft dot net
Operating system:
PHP version: 7.0.13
Package: Performance problem
Bug Type: Bug
Bug description:foreach() way slower than while(list($key,$value)=each($arr))
Description:
------------
since you depreacte each() with PHP 7.2 better fix foreach() before
[harry@srv-rhsoft:/downloads]$ php each.php
Extract: 0.031516075134277
While: 0.0004878044128418
For-Each: 0.11238217353821
Test script:
---------------
[harry@srv-rhsoft:/downloads]$ cat each.php
<?php
$loops = 2000;
for($x=1;$x<$loops;$x++)
{
$arr['key' . $x] = 'Test';
}
echo "Extract: ";
$start = microtime(true);
for($x=1;$x<$loops;$x++)
{
extract($arr, EXTR_SKIP);
}
echo microtime(true) - $start . "\n";
echo "While: ";
$start = microtime(true);
for($x=1;$x<$loops;$x++)
{
while(list($key,$value)=each($arr))
{
${$key} = $value;
}
}
echo microtime(true) - $start . "\n";
echo "For-Each: ";
$start = microtime(true);
for($x=1;$x<$loops;$x++)
{
foreach($arr as $key=>$value)
{
${$key} = $value;
}
}
echo microtime(true) - $start . "\n";
?>
--
Edit bug report at https://bugs.php.net/bug.php?id=73571&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=73571&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=73571&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=73571&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=73571&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=73571&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=73571&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=73571&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=73571&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=73571&r=support
Expected behavior: https://bugs.php.net/fix.php?id=73571&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=73571&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=73571&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=73571&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=73571&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=73571&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=73571&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=73571&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=73571&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=73571&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=73571&r=mysqlcfg