PhpRiot
News Archive
PhpRiot Newsletter
Your Email Address:

More information

iconv_substr vs mbstring_substr

Note: This article was originally published at Planet PHP on 30 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. A
  2. $str = str_repeat("foo",100000);
  3. $time = microtime(true);
  4. A
  5. iconv_substr($str,1000,90000,'UTF-8');
  6. A
  7. echo "iconv_substr: " . (microtime(true)-$time) . "\n";
  8. A
  9. $time = microtime(true);
  10. A
  11. mb_substr($str,1000,90000,'UTF-8');
  12. A
  13. echo "mb_substr: " . (microtime(true)-$time) . "\n";
  14. A
  15. $time = microtime(true);
  16. A
  17. substr($str,1000,90000);
  18. A
  19. echo "substr: " . (microtime(true)-$time) . "\n";
  20. ?

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)