iconv_substr vs mbstring_substr
Note: This article was originally published at Planet PHP
on 29 September 2011.
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
- A
- $str = str_repeat("foo",100000);
- $time = microtime(true);
- A
- iconv_substr($str,1000,90000,'UTF-8');
- A
- echo "iconv_substr: " . (microtime(true)-$time) . "\n";
- A
- $time = microtime(true);
- A
- mb_substr($str,1000,90000,'UTF-8');
- A
- echo "mb_substr: " . (microtime(true)-$time) . "\n";
- A
- $time = microtime(true);
- A
- substr($str,1000,90000);
- A
- 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 "/
Truncated by Planet PHP, read more at the original (another 1494 bytes)


