note 94066 added to function.explode
| From: | nickdotbrownatfreedotfr at osu1 dot php dot net | Date: | Wed, 14 Oct 2009 22:45:57 +0000 |
| Subject: | note 94066 added to function.explode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-161740@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 69.147.83.197
Probable Submitter: 88.160.64.2
----
Manual Page -- http://www.php.net/manual/en/function.explode.php
Edit -- https://master.php.net/note/edit/94066
Del: integrated -- https://master.php.net/note/delete/94066/integrated
Del: useless -- https://master.php.net/note/delete/94066/useless
Del: bad code -- https://master.php.net/note/delete/94066/bad+code
Del: spam -- https://master.php.net/note/delete/94066/spam
Del: non-english -- https://master.php.net/note/delete/94066/non-english
Del: in docs -- https://master.php.net/note/delete/94066/in+docs
Del: other reasons-- https://master.php.net/note/delete/94066
Reject -- https://master.php.net/note/reject/94066
Search -- https://master.php.net/manage/user-notes.php