note 94068 deleted from function.explode by salathe

From: Date: Mon, 21 Sep 2020 14:50:17 +0000
Subject: note 94068 deleted from function.explode by salathe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-215343@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, as it can also explode your memory usage.

« previous php.notes (#215343) next »