PHP Character Segmentation explode,split,preg_split Performance Comparison

Source: Internet
Author: User

PHP Character Segmentation explode,split,preg_split Performance Comparison

Three functions are used to split a string, and several experiments are performed to compare the performance.

1. Comparison between explode and split

are split with characters and executed 10 times

Code:

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);}

Results:

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

Conclusion: The explode performance is better than split in the case that all characters are separated.

Split is segmented with a regular:

Code:

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);}


Results:

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

Conclusion: In the case of split using regular segmentation, the performance of the two is comparable

2. Split vs. Preg_split comparison

Code:

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);}


Results:

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

Conclusion: Split performance is better than preg_split

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.