分类: php2012-07-12 09:46 1109人阅读 评论(1) 收藏 举报

三个函数都是用来对字符串进行分割,下面分几个实验来比较之间的性能。

1. explode与split比较

都用字符进行分割,执行10次

代码:

  1. for($num=0;$num<9;$num++)
  2. {
  3. $stime=microtime();
  4. for($i=0;$i<10000;$i++)
  5. {
  6. explode(',','hello,world');
  7. }
  8. $etime=microtime();
  9. printf("explode takes %f \n",$etime-$stime);
  10. }
  11. for($num=0;$num<9;$num++)
  12. {
  13. $stime=microtime();
  14. for($i=0;$i<10000;$i++)
  15. {
  16. split(',','hello,world');
  17. }
  18. $etime=microtime();
  19. printf("split takes %f \n",$etime-$stime);
  20. }

结果:

explode takes 0.009193 
explode takes 0.007066 
explode takes 0.007623 
explode takes 0.005829 
explode takes 0.005497 
explode takes 0.005734 
explode takes 0.005502 
explode takes 0.005515 
explode takes 0.005524 
split takes 0.008976 
split takes 0.008971 
split takes 0.009030 
split takes 0.009173 
split takes 0.009913 
split takes 0.012656 
split takes 0.012153 
split takes 0.009161 
split takes 0.008948

结论:都用字符进行分割的情况下,explode性能优于split

split用正则进行分割:

代码:

  1. for($num=0;$num<9;$num++)
  2. {
  3. $stime=microtime();
  4. for($i=0;$i<10000;$i++)
  5. {
  6. explode(',','hello,world');
  7. }
  8. $etime=microtime();
  9. printf("explode takes %f \n",$etime-$stime);
  10. }
  11. for($num=0;$num<9;$num++)
  12. {
  13. $stime=microtime();
  14. for($i=0;$i<10000;$i++)
  15. {
  16. split('/,/','hello,world');
  17. }
  18. $etime=microtime();
  19. printf("split takes %f \n",$etime-$stime);
  20. }

结果:

explode takes 0.005649
explode takes 0.005719
explode takes 0.005792
explode takes 0.005975
explode takes 0.005723
explode takes 0.005812
explode takes 0.005860
explode takes 0.005756
explode takes 0.005835
split takes 0.005158 
split takes 0.005170 
split takes 0.005440 
split takes 0.005402 
split takes 0.005272 
split takes 0.005434 
split takes 0.006389 
split takes 0.005414 
split takes 0.005510

结论:在split使用正则分割的情况下,两者性能不相上下

2. split与preg_split比较

代码:

  1. for($num=0;$num<9;$num++)
  2. {
  3. $stime=microtime();
  4. for($i=0;$i<10000;$i++)
  5. {
  6. split('/,/','hello,world');
  7. }
  8. $etime=microtime();
  9. printf("split takes %f \n",$etime-$stime);
  10. }
  11. for($num=0;$num<9;$num++)
  12. {
  13. $stime=microtime();
  14. for($i=0;$i<10000;$i++)
  15. {
  16. preg_split("/,/","hello,world");
  17. }
  18. $etime=microtime();
  19. printf("preg_split takes %f \n",$etime-$stime);
  20. }

结果:

split takes 0.005381 
split takes 0.005568 
split takes 0.005107 
split takes 0.005097 
split takes 0.005444 
split takes 0.005538 
split takes 0.005095 
split takes 0.005112 
split takes 0.005087 
preg_split takes 0.009364 
preg_split takes 0.009568 
preg_split takes 0.009037 
preg_split takes 0.008957 
preg_split takes 0.009021 
preg_split takes 0.009656 
preg_split takes 0.009038 
preg_split takes 0.008962 
preg_split takes 0.009412

结论:split性能优于preg_split

explode,split,preg_split性能比较的更多相关文章

  1. split(),preg_split()与explode()函数分析与介

    split(),preg_split()与explode()函数分析与介 发布时间:2013-06-01 18:32:45   来源:尔玉毕业设计   评论:0 点击:965 split()函数可以实 ...

  2. Deprecated: Function split() is deprecated in ... 解决办法

    本地测试的程序上传到服务器出现很多错误,Deprecated: Function split() is deprecated  查了原因是因为PHP的版本不同所导致的,本身程序开发的时候用的是PHP5 ...

  3. PHP:错误 Deprecated: Function split() is deprecated in ... 解决办法

    PHP:错误 Deprecated: Function split() is deprecated in ... 解决办法 PHP5.3 split() 不建议使用的原因:PHP 5.3.0 之后的r ...

  4. php Function split() is deprecated 的解决办法

    原文地址: http://www.cnblogs.com/mfryf/archive/2012/05/31/2527307.html php升级为5.3后,程序会报 Function split() ...

  5. 转义字符\(在hive+shell以及java中注意事项):正则表达式的转义字符为双斜线,split函数解析也是正则

    转义字符 将后边字符转义,使特殊功能字符作为普通字符处理,或者普通字符转化为特殊功能字符. 各个语言中都用应用,如java.python.sql.hive.shell等等. 如sql中 "\ ...

  6. Hive表生成函数explode讲解

    Hive中的表分析函数接受零个或多个输入,然后产生多列或多行输出. 1.explode函数 explode函数以array类型数据输入,然后对数组中的数据进行迭代,返回多行结果,一行一个数组元素值 A ...

  7. hive lateral view 与 explode详解

    ref:https://blog.csdn.net/bitcarmanlee/article/details/51926530 1.explode hive wiki对于expolde的解释如下: e ...

  8. hive splict, explode, lateral view, concat_ws

    hive> create table arrays (x array<string>) > row format delimited fields terminated by ...

  9. PHP之string之explode()函数使用

    explode (PHP 4, PHP 5, PHP 7) explode - Split a string by string explode - 使用一个字符串分割另一个字符串 Descripti ...

随机推荐

  1. Windows 10通过本地镜像离线安装.NET 3.5

    在Windows10中,当我们安装某些软件的时候会提示"你的电脑上的应用需要使用以下Windows功能:.NET Framework 3.5(包括.NET 2.0和3.0)",由于 ...

  2. .net学习笔记--使用抽象方法实现多态

    在使用抽象方法实现多态之前,我们必须知道一些知识点: 1.抽象类不能被实例化: 2.抽象类可以包含非抽象成员,它们可以由其子类继承调用. 我们可以先创建一个Person的抽象类,代码如下: abstr ...

  3. PowerShell全自动分配CPU

    代码 $pro = Get-Process foreach ($n in $pro){ if($n.ProcessorAffinity -ne 255){ #continue } if($n.Id - ...

  4. Lua 单例类

    function SingleTon:new() local store = nil return function(self) if store then return store end loca ...

  5. Module 'fileinfo' already loaded in Unknown on line 0

    出现的原因是:需要加载的扩展已经以而二进制文件的形式写入了php中,但是,在php.ini中却再一次动态加载 参考出处

  6. httpServletRequest对象、filter、servlet、servlet容器、catalina、tomcat、以及web容器之间的关系

    学习servlet的时候经常感到疑惑 HttpServletRequest是服务器创建的?还是servlet容器创建的? 过滤器是服务器创建的?还是servlet容器创建的? serlet容器和tom ...

  7. kendo chart label position 图表的值标签位置及显示模板

    1.不显示0 seriesDefaults: { type: "column", labels: { visible: true, position:'' background: ...

  8. TCP/IP协议和HTTP协议 浩哥指教

    TCP和IP在HTTP协议的上层,HTTP算是应用层,IP协议建立的是电脑跟电脑之间的联系,具体过程是,物理上,通过网线,解析MAC地址,到达路由,路由告诉数据将要去哪里,对方电脑通过NDS解析,解析 ...

  9. 【转】当你在浏览器地址栏输入一个URL后回车,将会发生的事情?

    http://igoro.com/archive/what-really-happens-when-you-navigate-to-a-url/ http://www.cnblogs.com/panx ...

  10. Angular常用功能

    1.默认选择让第0个元素的class为active ng-class="{active:$index == 0}" 2.指令的例子 <!DOCTYPE html> &l ...