note 94066 deleted from function.explode by danbrown
| From: | danbrown@php.net | Date: | Thu, 15 Oct 2009 00:01:00 +0000 |
| Subject: | note 94066 deleted from function.explode by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-161743@lists.php.net to get a copy of this message | ||
Note Submitter: nick dot brown at free dot fr
----
My application was running out of memory (my hosting company limits PHP to 32MB). I have a string
containing between 100 and 20000 triplets, separated by a space, with each triplet consisting of
three double-precision numbers, separated by commas. Total size of the biggest string, with 20000
triplets, is about 1MB.
The application needs to split the string into triplets, then split the triplet into numbers. In C,
this would take up about 480K (20000 times 3 x 8 bytes) for the final array. The intermediate array
of strings shouldn't be much bigger than the long string itself (1MB). And I expect some
overhead from PHP, say 300% to allow for indexes etc.
Well, PHP5 manages to run out of memory *at the first stage* (exploding the string on the space
character). I'm expecting to get an array of 20000 strings, but it needs more than 32MB to
store it. Amazing.
The workaround was easy and had the bonus of producing faster code (I compared it on a 10000 triplet
string). Since in any case I had to split up the numeric triplets afterwards, I decided to use
preg_match_all() on the original string. Despite the fact that the resulting "matches"
array contains more data per element than the result of explode() - because it stores the matched
triplet, plus its component numbers - it takes up far less memory.
Moral: be careful when using explode() on big strings, it can also explode your memory usage.