#18829 [Opn]: array_pop, array_shift, array_push... functions very slow with large arrays
| From: | rodif_bl@php.net | Date: | Mon, 12 Aug 2002 13:18:57 +0000 |
| Subject: | #18829 [Opn]: array_pop, array_shift, array_push... functions very slow with large arrays | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-16538@lists.php.net to get a copy of this message | ||
ID: 18829
Updated by: rodif_bl@php.net
Reported By: jean-charles.rogez@devoteam.com
Status: Open
Bug Type: Arrays related
Operating System: Linux RedHat 7.1
PHP Version: 4.2.2
New Comment:
The problem wasn't just array_shift it was array_shift and array_pop.
array_shift is whats causing the slowness now but way its implemented
now I really don't think it can get much faster. Basically when you
shift of the top of the array it needs to re-index the entire array
meaning if you have 10,000 records it needs to loop thru all 10,000
changing the index. If the function didn't need to do this it wouldn't
take nearly as long. Does it work this way in perl or python.
$a = array(1,2);
array_shift($a);
echo $a[0];
Previous Comments:
------------------------------------------------------------------------
[2002-08-12 04:25:03] jean-charles.rogez@devoteam.com
Same results than you. The script takes approximately 4 minutes on my
P4 1.5Ghz with the latest CVS (before it was 87m...). After correction
of the array_shift function, PHP should be in the same time than Perl
or Python.
------------------------------------------------------------------------
[2002-08-10 05:39:19] edink@php.net
I did some testing on your function and it seems that array_shift is to
blame. If I avoid using that function, the execution time drop down to
few seconds. To cut down the problem:
$size = 5000;
$Li1 = array();
$Li1 = array_pad($Li1, $size, 0);
$Li2 = array();
while ($Li1) {
$Li2[] = array_shift($Li1);
}
This takes 1.8 s on my P3 933 MHz. If I increase $size to 10000 it
takes 16 s.
The rest of array functions seem to be working fine.
------------------------------------------------------------------------
[2002-08-09 12:59:50] rodif_bl@php.net
I ran current cvs and it was alot faster but still not fast enough.
with your example with 50 itterations
4m55.514s
1 itteration was about
5 seconds
this is running on 1.2 p3
------------------------------------------------------------------------
[2002-08-09 09:03:01] rodif_bl@php.net
This bug has been fixed in CVS. You can grab a snapshot of the
CVS version at http://snaps.php.net/. In case this was a
documentation
problem, the fix will show up soon at http://www.php.net/manual/.
In case this was a PHP.net website problem, the change will show
up on the PHP.net site and on the mirror sites.
Thank you for the report, and for helping us make PHP better.
I recently fixed the speed for array_shift and array_pop. I\'ll look at
array_push see if there is anything good there. Try current CVS and
tell me if the speed increased for you.
------------------------------------------------------------------------
[2002-08-09 07:29:25] jean-charles.rogez@devoteam.com
Oops ! I forgot the end of the script
Add
while ($ITER-- > 0) {
$result = test_lists();
}
print "$result\n";
?>
In the previous comment, N meant ITER. (ITER = 50)
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
http://bugs.php.net/18829
--
Edit this bug report at http://bugs.php.net/?id=18829&edit=1