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_ ...
随机推荐
- 为什么你不应该用angularjs?
AngularJS的问题 为什么你不该用angularjs,https://medium.com/@mnemon1ck/why-you-should-not-use-angularjs-1df5ddf ...
- 【BZOJ-1857】传送带 三分套三分
1857: [Scoi2010]传送带 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 1077 Solved: 575[Submit][Status][ ...
- ueditor的优酷插件模式开发,目前开发了腾讯视频转换插件
项目相关地址 源码:https://github.com/easonjim/ueditor_plugin bug提交:https://github.com/easonjim/ueditor_plugi ...
- C#把数据写到硬盘指定位置
FileStream fs; StreamWriter sw;string RootPath = @"C:\Advantech" + "\\tempData\\" ...
- POJ 1258 Agri-Net(最小生成树 Prim+Kruskal)
题目链接: 传送门 Agri-Net Time Limit: 1000MS Memory Limit: 10000K Description Farmer John has been elec ...
- Ubuntu回收站
以前删除文件经常Move to trash,今天想清空发现根本不知道回收站在哪里,囧.遂Google之,于是发现在 -/.local/share/Trash目录下. 打开目录看看有什么东西: ➜ ~ ...
- log4net配置和获取ILog实例
名称 描述 File 文件路径,如果RollingStyle为Composite或Date,则这里设置为目录,文件名在DatePattern里设置,其他则这里要有文件名.已经扩展支持虚拟目录 Roll ...
- ejb 远程调用
1,客户端代码: package com.example.test; import java.util.Hashtable; import java.util.Properties; import j ...
- HDU1024Max Sum Plus Plus(M段最大和)
题意:求一个数组中 M 段的 最大和 没看明白怎么搞得 抽空来看,写的不赖 #include <iostream> #include <cstring> #include &l ...
- SQL Server编程(04)基本语法【转载】
一.定义变量 --简单赋值 declare @a int set @a=5 print @a --使用select语句赋值 declare @user1 nvarchar(50) select @ ...