iconv_substr vs mbstring_substr in PHP
Join the DZone community and get the full member experience.
Join For FreeWhile working on an application I ran across a huge bottleneck which I
isolated down all the way to the use of the iconv_substr function. If
you ever wonder which is better to use, this should help your decision:
Benchmark script
<?php $str = str_repeat("foo",100000); $time = microtime(true); iconv_substr($str,1000,90000,'UTF-8'); echo "iconv_substr: " . (microtime(true)-$time) . "\n"; $time = microtime(true); mb_substr($str,1000,90000,'UTF-8'); echo "mb_substr: " . (microtime(true)-$time) . "\n"; $time = microtime(true); substr($str,1000,90000); echo "substr: " . (microtime(true)-$time) . "\n"; ?>
The results widely varied between machines, operating systems and PHP versions; but here are two results I recorded.
First, PHP 5.3.4 on OS/X:
iconv_substr: 0.014400005340576 mb_substr: 0.00049901008605957 substr: 3.7193298339844E-5 # Note the E-notation, this was actually something like 0.00003 seconds.
As you can see iconv took 0.01 seconds, while mbstring took only 0.0004 seconds. Already a significant difference (2800% slower), but the difference became more apparent when running this on a Debian box with PHP 5.2.13.
iconv_substr: 8.3735179901123 mb_substr: 0.00039505958557129 substr: 4.8160552978516E-5
Yup, it took 8.3 seconds. That's an increase of over 2100000%. So next time you're wondering which of the two may be smarter to use, this may help you decide.
Important to note that OS/X uses libiconv as the 'iconv implementation' and my Debian test machine 'glibc', so it looks like libiconv is much, much faster than glibc. mbstring still leaves both in the dust though.
I'm interested to hear what your results are, especially if they differ.
Published at DZone with permission of Evert Pot, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Automating the Migration From JS to TS for the ZK Framework
-
Effortlessly Streamlining Test-Driven Development and CI Testing for Kafka Developers
-
Never Use Credentials in a CI/CD Pipeline Again
-
5 Key Concepts for MQTT Broker in Sparkplug Specification
Comments