Bug #18829 [Com]: array_pop, array_shift, array_push... functions very slow with large arrays
| From: | tszming at gmail dot com | Date: | Wed, 20 Jun 2012 16:45:11 +0000 |
| Subject: | Bug #18829 [Com]: 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-171387@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=18829&edit=1
ID: 18829
Comment by: tszming at gmail dot com
Reported by: jean-charles dot rogez at devoteam dot com
Summary: array_pop, array_shift, array_push... functions very
slow with large arrays
Status: Wont fix
Type: Bug
Package: Arrays related
Operating System: Linux RedHat 7.1
PHP Version: 4.2.2
Assigned To: rodif_bl
Block user comment: N
Private report: N
New Comment:
Maybe some optimization can be done if we know a variable is an array but not a
hash?
I believe JavaScript also share similar issue and they got optimized finally, e.g:
https://bugzilla.mozilla.org/show_bug.cgi?id=394448
Previous Comments:
------------------------------------------------------------------------
[2002-10-28 14:05:54] sterling@php.net
PHP is not perl, since arrays are bastards (the unwed child of a hash and array) there is really not
a more efficient way to handle this, without breaking things badly...
------------------------------------------------------------------------
[2002-08-13 20:45:48] kalowsky@php.net
Assigning to Brad, as he seems to be working on it... a bit.
------------------------------------------------------------------------
[2002-08-13 04:11:32] jean-charles dot rogez at devoteam dot com
In perl :
@a = (1, 2, 3);
shift(@a);
print "$a[0], $a[1]";
Result : 2, 3
This is correct.
I don't know how the shift function is implemented in perl, but i think arrays are not
reindexed like in PHP, else it shouldn't be so fast.
------------------------------------------------------------------------
[2002-08-12 09:18:55] rodif_bl@php.net
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];
------------------------------------------------------------------------
[2002-08-12 04:25:03] jean-charles dot rogez at devoteam dot 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.
------------------------------------------------------------------------
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
https://bugs.php.net/bug.php?id=18829
--
Edit this bug report at https://bugs.php.net/bug.php?id=18829&edit=1