note 94068 added to function.explode

From: Date: Wed, 14 Oct 2009 22:47:32 +0000
Subject: note 94068 added to function.explode
Groups: php.notes 
Request: Send a blank email to php-notes+get-161742@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, as 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/94068 Del: integrated -- https://master.php.net/note/delete/94068/integrated Del: useless -- https://master.php.net/note/delete/94068/useless Del: bad code -- https://master.php.net/note/delete/94068/bad+code Del: spam -- https://master.php.net/note/delete/94068/spam Del: non-english -- https://master.php.net/note/delete/94068/non-english Del: in docs -- https://master.php.net/note/delete/94068/in+docs Del: other reasons-- https://master.php.net/note/delete/94068 Reject -- https://master.php.net/note/reject/94068 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#161742) next »