Home >Backend Development >PHP Tutorial >PHP字符分割explode,split,preg_split性能比较

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

WBOY
WBOYOriginal
2016-06-20 13:00:351450browse

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>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:php魔法函数与变量Next article:PHP系统常量