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

php split for,PHP字符分割explode,split,preg_split性能比较_第1张图片

关于microtime()函数的问题我分别在程序的开头和结尾用microtime()函数取得当前时间,然后相减得到程序运行时间,在5.2版本的php上没问题,但是在5.3、5.4和5.5三个版本的php相减结果不是0就是0.0156,跟5.2.6版本的完全不同,这是...

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

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

1. explode与split比较

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

代码:

for($num=0;$num<9;$num++)

{

$stime=microtime();

for($i=0;$i<10000;$i++)

{

explode(',','hello,world');

}

$etime=microtime();

printf("explode takes %f \n",$etime-$stime);

}

for($num=0;$num<9;$num++)

{

$stime=microtime();

for($i=0;$i<10000;$i++)

{

split(',','hello,world');

}

$etime=microtime();

printf("split takes %f \n",$etime-$stime);

}

结果:

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用正则进行分割:

代码:

for($num=0;$num<9;$num++)

{

$stime=microtime();

for($i=0;$i<10000;$i++)

{

explode(',','hello,world');

}

$etime=microtime();

printf("explode takes %f \n",$etime-$stime);

}

for($num=0;$num<9;$num++)

{

$stime=microtime();

for($i=0;$i<10000;$i++)

{

split('/,/','hello,world');

}

$etime=microtime();

printf("split takes %f \n",$etime-$stime);

}

结果:

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比较

代码:

for($num=0;$num<9;$num++)

{

$stime=microtime();

for($i=0;$i<10000;$i++)

{

split('/,/','hello,world');

}

$etime=microtime();

printf("split takes %f \n",$etime-$stime);

}

for($num=0;$num<9;$num++)

{

$stime=microtime();

for($i=0;$i<10000;$i++)

{

preg_split("/,/","hello,world");

}

$etime=microtime();

printf("preg_split takes %f \n",$etime-$stime);

}

结果:

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

关于microtime()函数的问题我分别在程序的开头和结尾用microtime()函数取得当前时间,然后相减得到程序运行时间,在5.2版本的php上没问题,但是在5.3、5.4和5.5三个版本的php相减结果不是0就是0.0156,跟5.2.6版本的完全不同,这是...

...ray('12345', '67891'); $pattern6 = array('a', '1234567890'); $t = microtime(true); for($i=0; $i

...10次

代码:

for($num=0;$num

...>使用array_push压入1000000个元素

$starttime = get_microtime();

$arr = array();

for($i=0; $i<1000000; $i++){

array_push($arr, $i);

}

$endtim

一道关于microtime()的题目php获取时间的函数$gtc_end_time = microtime(true); 从下面这种数列中(1000个吧),19493845809,24359390850934,3435345,4345345,5345345,6345345,73453453 ……

...n5 = array('12345', '67891');

$pattern6 = array('a', '1234567890');

$t = microtime(true);

for($i=0; $i

在php中如果我们要获取毫秒就必须通过microtime()再进行转换,下面我来给各位朋友举几个实例,希望对大家会有所帮助。问题不怕弱智…啥都记

定义和用法PHP函数microtime()返回当前 Unix 时间戳和微秒数。PHP函数microtime()语法microtime(get_as_float)PHP函数microtime()参数与描述get_as_float 如果给出了 get_as_float 参数并且其值等价于 TRUE,该函

microtime()函数是php中的一个内置函数,用于以微秒为单位返回当前的unix时间戳。该函数接收单个可选参数get-as-float,默认返回微秒数字符串。php microtime()函数怎么用?php microtime()函数返回...

...meSpent = 0; function start(){ $this->StartTime = microtime(); } function stop(){ $this->StopTime = microtime(); } fun

你可能感兴趣的:(php,split,for)