转自: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. Leetcode Power of two, three, four

    Given an integer, write a function to determine if it is a power of two. Hint: Could you solve it in ...

  2. Slave2: no datanode to stop(HADOOP_PID_DIR)

    HADOOP_PID_DIR 本想在环境变量里设置,在相关文件里直接尹用,但是我想起来那时候的JAVA_HOME都不行,还是一个一个设置吧. 有时候,我们对运行几天或者几个月的hadoop或者hbas ...

  3. C++自问自答

    1.为什么派生层次上的类,同一个虚函数在各个类的虚表中的位置一样?         因为:对虚函数的调用是通过虚指针+偏移地址构成,由于对虚函数的调用都是通过这种方式,所以对同一个虚函数的偏移值就必须 ...

  4. PHP守护进程

    php也是可以直接进行守护进程的启动与终止的,相对于shell来说会简单很多,理解更方便,当然了php的守护进程要实现自动重启还是要依赖于shell的crontab日程表,每隔一段时间去执行一次脚本看 ...

  5. primefaces p:tableData 显示 List<List>

    @javax.faces.bean.ViewScoped public class Controlador { private List<List> estadistico; @PostC ...

  6. tcpdump抓包命令

    本文转自 : http://www.cnblogs.com/ggjucheng/archive/2012/01/14/2322659.html http://www.itshouce.com.cn/l ...

  7. 通过命令行连接Wifi

    前提:无线网卡驱动正常安装 1.检查连接无线的接口 $ iwconfig 一般无线接口为wlan0 2.检查无线接口是否工作 $ sudo ip link set wlan0 up 3.扫描周围无线网 ...

  8. CSS3中的animation动画

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  9. 延时Led灯

    1.代码:#include <reg52.h>typedef unsigned int  u16;typedef unsigned char u8;sbit led = P1^0;void ...

  10. ubuntu rhythmbox乱码解决方法

    因为安装的是双系统,所以音乐文件在win的盘下面,所以采用的方法是 1. 首先,需要有软件包mid3iconv.如果你的系统中没有安装它,可以通过如下代码自动安装:sudo apt-get insta ...