note 78494 deleted from function.explode by peehaa
| From: | peehaa@php.net | Date: | Sat, 10 Dec 2016 14:12:05 +0000 |
| Subject: | note 78494 deleted from function.explode by peehaa | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-208455@lists.php.net to get a copy of this message | ||
Note Submitter: pinkgothic at gmail dot com
----
coroa at cosmo-genics dot com mentioned using preg_split() instead of explode() when you have
multiple delimiters in your text and don't want your result array cluttered with empty
elements. While that certainly works, it means you need to know your way around regular
expressions... and, as it turns out, it is slower than its alternative. Specifically, you can cut
execution time roughly in half if you use array_filter(explode(...)) instead.
Benchmarks (using 'too many spaces'):
Looped 100000 times:
preg_split: 1.61789011955 seconds
filter-explode: 0.916578054428 seconds
Looped 10000 times:
preg_split: 0.162719011307 seconds
filter-explode: 0.0918920040131 seconds
(The relation is, evidently, pretty linear.)
Note: Adding array_values() to the filter-explode combination, to avoid having those oft-feared
'holes' in your array, doesn't remove the benefit, either. (For scale - the
'9' becomes a '11' in the benchmarks above.)
Also note: I haven't tested anything other than the example with spaces - since djogo_curl at
yahoo's note seems to imply that explode() might get slow with longer delimiters, I expect this
would be the case here, too.
I hope this helps someone. :)