>  기사  >  백엔드 개발  >  PHP字符分割explode,split,preg_split性能比较

PHP字符分割explode,split,preg_split性能比较

WBOY
WBOY원래의
2016-06-20 13:00:351411검색

PHP字符分割explode,split,preg_split性能比较

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

1. explode与split比较

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

代码:

for($num=0;$num

<p>结果:</p>

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

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

<p> </p>

<p>split用正则进行分割:</p>

<p>代码:</p>

<pre class="brush:php;toolbar:false">for($num=0;$num

<p><br>
结果:</p>

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

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

<p>2. split与preg_split比较</p>

<p>代码:</p>

<pre class="brush:php;toolbar:false">for($num=0;$num

<p><br>
结果:</p>

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

<p>结论:split性能优于preg_split</p><p class="item-note"><br></p>
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:php魔法函数与变量다음 기사:PHP系统常量