Bug #77556 [NEW]: COUNT_RECURSIVE is suprisingly slow
| From: | ivan dot enderlin at hoa-project dot net | Date: | Fri, 01 Feb 2019 14:52:47 +0000 |
| Subject: | Bug #77556 [NEW]: COUNT_RECURSIVE is suprisingly slow | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-219328@lists.php.net to get a copy of this message | ||
From: ivan dot enderlin at hoa-project dot net
Operating system: macOS
PHP version: 7.3.1
Package: Performance problem
Bug Type: Bug
Bug description:COUNT_RECURSIVE is suprisingly slow
Description:
------------
I discovered
COUNT_RECURSIVE and thought it would speed up my
algorithm. But instead, it slowed it down. It initially used array_sum
+ array_map. See the test script.
This result is surprising because array_map creates a new array, so
the memory allocations should be slower than just iterating and
counting. It proofs to be true with the last test in the script (see
âmanualâ) where it is faster than all the previous solutions.
Test script:
---------------
<?php
const LENGTH = 100000;
$a = array_fill(0, LENGTH, array_fill(0, LENGTH, 42));
echo 'expected: '; var_dump(LENGTH * LENGTH);
echo "\n\n";
$_ = microtime(true);
echo 'count recursive: '; var_dump(count($a, COUNT_RECURSIVE) -
count($a));
echo 'time: ', number_format((microtime(true) - $_) / 1000, 6), "\n\n";
$_ = microtime(true);
echo 'sum + map: '; var_dump(array_sum(array_map('count',
$a)));
echo 'time: ', number_format((microtime(true) - $_) / 1000, 6), "\n\n";
$_ = microtime(true);
echo 'sum + map + closure: '; var_dump(array_sum(array_map(function
(array $v) { return count($v); }, $a)));
echo 'time: ', number_format((microtime(true) - $_) / 1000, 6), "\n\n";
$_ = microtime(true);
echo 'manual: ';
$i = 0;
foreach ($a as $aa) {
$i += count($aa);
}
var_dump($i);
echo 'time: ', number_format((microtime(true) - $_) / 1000, 6), "\n\n";
/**
* Output:
*
* expected: int(10000000000)
*
*
* count recursive: int(10000000000)
* time: 0.012170
*
* sum + map: int(10000000000)
* time: 0.000005
*
* sum + map + closure: int(10000000000)
* time: 0.000007
*
* manual: int(10000000000)
* time: 0.000002
*/
Expected result:
----------------
COUNT_RECURSIVE should be as fast as or faster than array_sum +
array_map.
COUNT_RECURSIVE should be as fast as the âmanualâ test, or even
faster because it's written in C directly.
Actual result:
--------------
COUNT_RECURSIVE is way too slow :-).
--
Edit bug report at https://bugs.php.net/bug.php?id=77556&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=77556&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=77556&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=77556&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=77556&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=77556&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=77556&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=77556&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=77556&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=77556&r=support
Expected behavior: https://bugs.php.net/fix.php?id=77556&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=77556&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=77556&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=77556&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=77556&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=77556&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=77556&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=77556&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=77556&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=77556&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=77556&r=mysqlcfg