转自:http://benchmarks.ro/2011/02/str_replace-vs-preg_replace/

事实证明str_replace确实比preg_replace快。

If you find yourself handling strings and replacing sub-strings within them you want to make sure that you are using the fastest way possible to do it.
In order to help you we ran tests using an average length string (50k characters) and 100*1000 test runs.
After running these tests we concluded that str_replace is faster thanpreg_replace when it comes to simple replace rules, these being our results for two separate full test runs:

Time for str_replace: 2.2866821289062 seconds
Time for preg_replace: 2.9614992141724 seconds

Time for str_replace: 2.471617937088 seconds
Time for preg_replace: 3.2965259552002 seconds

You can see the code we used for benchmarking below:

<?php

//Build string to be used
$max_elements=; $max_runs=; $test_string='';
for($i=;$i<$max_elements;$i++) $test_string.=chr(rand(,)); function test_str_replace()
{
global $test_string,$max_runs;
for($i=;$i<$max_runs;$i++) $test_string=str_replace(' ',' ',$test_string);
} function test_preg_replace()
{
global $test_string,$max_runs;
for($i=;$i<$max_runs;$i++) $test_string=preg_replace("/[ ]/",' ',$test_string);
} $time = microtime(true);
for($i=;$i<$max_runs;$i++) test_str_replace();
$time = microtime(true) - $time;
echo "Time for str_replace: {$time} seconds<br>\n"; $time = microtime(true);
for($i=;$i<$max_runs;$i++) test_preg_replace();
$time = microtime(true) - $time;
echo "Time for preg_replace: {$time} seconds<br>\n"; ?>

str_replace vs preg_replace的更多相关文章

  1. 可前端解密的加密方法探讨和str_replace和preg_replace分析

    目的: 对字符串‘123456’进行后端加密,前端js可解密出真实字符 测试代码php: static $hashMap = array( '0' => '4', '1' => '9', ...

  2. strtr、str_replace()、substr_replace、preg_replace之间的区别

    strtr(string, from, to): 逐个字符开始替换,以from跟to中长度较较短的一个为准,例如: strtr("aidengni","ai", ...

  3. php preg_replace空格无法替换问题

    一次坑爹的小bug.读取一段文字(编码utf-8),想替换掉空格,str_replace(" "..).preg_replace("/\s/"..)都不起作用. ...

  4. 提高PHP代码质量的36个技巧

    1.不要使用相对路径 常常会看到: require_once('../../lib/some_class.php'); 该方法有很多缺点: 它首先查找指定的php包含路径, 然后查找当前目录. 因此会 ...

  5. PHP代码20个实用技巧(转)

    这些技巧特别是封装的,相对路径的还是挺好的,本身来自微信公众号,但是我担心以后删除,所以在我的博客上备份一下(微信公众号为:菜鸟教程) 在这篇文章中我们将看看一些关于PHP开发有用的提示和技巧,可以用 ...

  6. php注意事项2

    1.不要使用相对路径 常常会看到: require_once('../../lib/some_class.php'); 该方法有很多缺点: 它首先查找指定的php包含路径, 然后查找当前目录. 因此会 ...

  7. php字符串赋值到js的坑

    很早以前的一个比较坑的问题,今天又遇到了,记录一下,免得以后再次入坑. 把php赋值到view层时,如果不是直接渲染到页面,而是赋值给变量.字符如果有回车或者换行就会出现问题. 示例: <?ph ...

  8. 推荐的PHP编码规范

    推荐的PHP编码规范 发布时间: 2014-05-7 浏览次数:2754 分类: PHP教程 推荐的PHP编码规范 一 编辑器设置 1. 使用Tab缩进,不要使用空格 鉴于很多编辑器在保存文件时会自动 ...

  9. 提高PHP开发质量的36个方法(精品)

    提高PHP开发质量的36个方法 林涛 发表于:2016-3-25 0:00 分类:26点 标签: 62次 1.不要使用相对路径 常常会看到: require_once('../../lib/some_ ...

随机推荐

  1. 【BZOJ-4568】幸运数字 树链剖分 + 线性基合并

    4568: [Scoi2016]幸运数字 Time Limit: 60 Sec  Memory Limit: 256 MBSubmit: 238  Solved: 113[Submit][Status ...

  2. UOJ149 【NOIP2015】子串

    本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转载请注明出处,侵权必究,保留最终解释权! [问题描述]有两个仅包含小写英文字母的字符串 A ...

  3. BZOJ3436 小K的农场

    Description 背景 小K是个特么喜欢玩MC的孩纸... 描述 小K在MC里面建立很多很多的农场,总共n个,以至于他自己都忘记了每个农场中种植作物的具体数量了,他只记得 一些含糊的信息(共m个 ...

  4. oracle创建用户、表空间、授权

    1.打开sqlplus 2.建表空间.用户.授权

  5. UVALive 3989Ladies' Choice(稳定婚姻问题)

    题目链接 题意:n个男生和女生,先是n行n个数,表示每一个女生对男生的好感值排序,然后是n行n列式每一个男生的好感值排序,输出N行,即每个女生在最好情况下的男生的编号 分析:如果是求女生的最好情况下, ...

  6. jquery selector 基础

    转自:http://www.cnblogs.com/zwl12549/archive/2008/08/09/1264163.html query的这套选择符是比较帅气的,借用了XPath2.0和CSS ...

  7. 在Android上实现使用Facebook登录(基于Facebook SDK 3.5)

    准备工作: 1.       Facebook帐号,国内开发者需要一个vpn帐号(网页可以浏览,手机可以访问) 2.       使用Facebook的SDK做应用需要一个Key Hashes值. 2 ...

  8. 初学者 的 js 关于checkbox全选的问题

    <script type="text/javascript" language="javascript" > function ff() { var ...

  9. MySQL中的外键是什么、有什么作用

    本文参加博文大赛,如果您满意的话麻烦点击这里给我投票原,查看原文点击这里.最近自学数据库MySQL,然后有个疑问,一直不得其解,查询了相关资料,最后还是没有解决. 我的疑问是 "使用外键约束 ...

  10. 监控web页面的性能指标。

    监控一个web页面的性能也是非常重要的,h5提供了一个非常好的属性来监控:  window.performance 它有两个成员:    navigation (一个叫做performanceNavi ...