PHP中strtr与str_replace函数运行性能简单测试示例

这篇文章主要介绍了PHP中strtr与str_replace函数运行性能简单测试,结合具体实例形式对比分析了PHP中strtr与str_replace函数的测试运行效率,需要的朋友可以参考下。

本文实例讲述了PHP中strtr与str_replace函数运行性能简单测试,分享给大家供大家参考,具体如下:

strtr与str_replace函数性能,很简单的一个测试,只是简单的测下,供参考,代码如下:

  1. <?php
  2. require_once('Timer.php');
  3. $target = 'qwertyuiop[]asdfghjkl;\'zxcvbnm,./qwertyuiop[]asdfghjkl;\'zxcvbnm,./qwertyuiop[]asdfghjkl;\'zxcvbnm,./qwertyuiop[]asdfghjkl;\'zxcvbnm,./qwertyuiop[]asdfghjkl;\'zxcvbnm,./qwertyuiop[]asdfghjkl;\'zxcvbnm,./qwertyuiop[]asdfghjkl;\'zxcvbnm,./qwertyuiop[]asdfghjkl;\'zxcvbnm,./qwertyuiop[]asdfghjkl;\'zxcvbnm,./qwertyuiop[]asdfghjkl;\'zxcvbnm,./';
  4. $count = isset($argv[1]) ? (int)$argv[1] : 1;
  5. $needle = 'vb';
  6. Timer::getInstance()->begin();
  7. for($i = 0; $i < $count; $i++) {
  8. strtr($target, $needle, '*');
  9. }
  10. echo "strtr exec {$count} times used time: " . Timer::getInstance()->end()->gone() . " sec.\n";
  11. //----------------------------------------------------------------------------------------------
  12. Timer::getInstance()->begin();
  13. for($i = 0; $i < $count; $i++) {
  14. str_replace($needle, '*', $target);
  15. }
  16. echo "str_replace exec {$count} times used time: " . Timer::getInstance()->end()->gone() . " sec.\n";

结果如下:

PHP中strtr与str_replace函数运行性能简单测试示例

那个正则替换的那个就不测了,应该是赶不上这两个的。