评论

收藏

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

开发技术 开发技术 发布于:2021-08-20 14:33 | 阅读数:523 | 评论:0

本文实例讲述了php中strtr与str_replace函数运行性能简单测试。分享给大家供大家参考,具体如下:
strtr与str_replace函数性能,很简单的一个测试,只是简单的测下,供参考,代码如下:
<?php
require_once('timer.php');
$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,./';
$count = isset($argv[1]) ? (int)$argv[1] : 1;
$needle = 'vb';
timer::getinstance()->begin();
for($i = 0; $i < $count; $i++) {
 strtr($target, $needle, '*');
}
echo "strtr exec {$count} times used time: " . timer::getinstance()->end()->gone() . " sec.\n";
//----------------------------------------------------------------------------------------------
timer::getinstance()->begin();
for($i = 0; $i < $count; $i++) {
 str_replace($needle, '*', $target);
}
echo "str_replace exec {$count} times used time: " . timer::getinstance()->end()->gone() . " sec.\n";
结果如下:
DSC0000.png

那个正则替换的那个就不测了,应该是赶不上这两个的。
希望本文所述对大家php程序设计有所帮助。
原文链接:https://blog.csdn.net/dclnet/article/details/48551657

关注下面的标签,发现更多相似文章