PhpRiot
Follow phpriot on Twitter
Sponsored Link
News Archive
PhpRiot Newsletter
Your Email Address:

More information

iconv_substr vs mbstring_substr

Note: This article was originally published at Planet PHP on 29 September 2011.
Planet PHP

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

  1. $str = str_repeat("foo",100000);
  2. $time = microtime(true);
  3. iconv_substr($str,1000,90000,'UTF-8');
  4. echo "iconv_substr: " . (microtime(true)-$time) . "\n";
  5. $time = microtime(true);
  6. mb_substr($str,1000,90000,'UTF-8');
  7. echo "mb_substr: " . (microtime(true)-$time) . "\n";
  8. $time = microtime(true);
  9. substr($str,1000,90000);
  10. echo "substr: " . (microtime(true)-$time) . "\n";
  11. ?

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:

  1. iconv_substr: 0.014400005340576
  2. mb_substr: 0.00049901008605957
  3. substr: 3.7193298339844E-5 "/

Truncated by Planet PHP, read more at the original (another 1494 bytes)