Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

iconv_substr vs mbstring_substr in PHP

DZone's Guide to

iconv_substr vs mbstring_substr in PHP

· Web Dev Zone
Free Resource

Make the transition to Node.js if you are a Java, PHP, Rails or .NET developer with these resources to help jumpstart your Node.js knowledge plus pick up some development tips.  Brought to you in partnership with IBM.

While 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.

Learn why developers are gravitating towards Node and its ability to retain and leverage the skills of JavaScript developers and the ability to deliver projects faster than other languages can.  Brought to you in partnership with IBM.

Topics:

Published at DZone with permission of Evert Pot, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}