str_replace vs preg_replace
转自: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的更多相关文章
- 可前端解密的加密方法探讨和str_replace和preg_replace分析
目的: 对字符串‘123456’进行后端加密,前端js可解密出真实字符 测试代码php: static $hashMap = array( '0' => '4', '1' => '9', ...
- strtr、str_replace()、substr_replace、preg_replace之间的区别
strtr(string, from, to): 逐个字符开始替换,以from跟to中长度较较短的一个为准,例如: strtr("aidengni","ai", ...
- php preg_replace空格无法替换问题
一次坑爹的小bug.读取一段文字(编码utf-8),想替换掉空格,str_replace(" "..).preg_replace("/\s/"..)都不起作用. ...
- 提高PHP代码质量的36个技巧
1.不要使用相对路径 常常会看到: require_once('../../lib/some_class.php'); 该方法有很多缺点: 它首先查找指定的php包含路径, 然后查找当前目录. 因此会 ...
- PHP代码20个实用技巧(转)
这些技巧特别是封装的,相对路径的还是挺好的,本身来自微信公众号,但是我担心以后删除,所以在我的博客上备份一下(微信公众号为:菜鸟教程) 在这篇文章中我们将看看一些关于PHP开发有用的提示和技巧,可以用 ...
- php注意事项2
1.不要使用相对路径 常常会看到: require_once('../../lib/some_class.php'); 该方法有很多缺点: 它首先查找指定的php包含路径, 然后查找当前目录. 因此会 ...
- php字符串赋值到js的坑
很早以前的一个比较坑的问题,今天又遇到了,记录一下,免得以后再次入坑. 把php赋值到view层时,如果不是直接渲染到页面,而是赋值给变量.字符如果有回车或者换行就会出现问题. 示例: <?ph ...
- 推荐的PHP编码规范
推荐的PHP编码规范 发布时间: 2014-05-7 浏览次数:2754 分类: PHP教程 推荐的PHP编码规范 一 编辑器设置 1. 使用Tab缩进,不要使用空格 鉴于很多编辑器在保存文件时会自动 ...
- 提高PHP开发质量的36个方法(精品)
提高PHP开发质量的36个方法 林涛 发表于:2016-3-25 0:00 分类:26点 标签: 62次 1.不要使用相对路径 常常会看到: require_once('../../lib/some_ ...
随机推荐
- u Calculate e阶乘相加求和问题
问题: (n < 10)写个算法 回答: 方法一: #include<stdio.h> int main() { int f[10]={1,1,2,6,24,120,7 ...
- 09-FZ6R 白色
- Git: 一些基本命令
1.快速获取远程项目 1) git clone xxx.git // 如:git clone git://git.kernel.org/pub/scm/git/git.git 2) git clone ...
- Linux 命令之 Navicat 连接 Linux 下的Mysql数据库
2016年12月7日18:44:06 -====------------------------ GRANT ALL PRIVILEGES ON *.* TO 'itoffice'@'%' IDEN ...
- PHP的单引号与双引号的区别
字符串应用 单引号 较好! 在某些特定情况下,单引号的效率比双引号高. PHP把单引号中的数据视为普通字符串,不再处理. 而双引号还要对其中的字符串进行处理,比如遇到$了会把其后的内容视为变量等.
- Parallel Computing–Cannon算法 (MPI 实现)
原理不解释,直接上代码 代码中被注释的源程序可用于打印中间结果,检查运算是否正确. #include "mpi.h" #include <math.h> #includ ...
- wireshark过滤语法总结-重点偏移过滤
http://chenjiji.com/post/3371.html 作者: CHAN | 发布: 2013 年 10 月 24 日 做应用识别这一块经常要对应用产生的数据流量进行分析. 抓包采用wi ...
- JavaScript中的事件对象
JavaScript中的事件对象 JavaScript中的事件对象是非常重要的,恐怕是我们在项目中使用的最多的了.在触发DOM上的某个事件时,会产生一个事件对象event,这个对象中包含这所有与事件有 ...
- jQuery小技巧
1. 禁止右键点击 $(document).bind("contextmenu",function(e){ return false; }); 2.隐藏搜索文本框文字 $(docu ...
- Yocto开发笔记之《快速入门,环境搭建 & 编译》(QQ交流群:519230208)
开了一个交流群,欢迎爱好者和开发者一起交流,转载请注明出处. QQ群:,为避免广告骚扰,申请时请注明 “开发者” 字样 ======================================== ...