Stristr (), Strstr (), Strpos () speed comparison in PHP

Source: Internet
Author: User

Speed Code:

<?php
function Getmicrotime ()
{
List ($usec, $sec) = Explode ("", Microtime ());
return (float) $usec + (float) $sec);
}

$time _start = Getmicrotime ();
$string = "Xxxddxx";
$find = "D";
for ($i =0; $i <300000; $i + +)
{
if (Stristr ($string, $find))//if (Strstr ($string, $find)) or if (Strpos ($string, $find))
{}
}
$time _end = Getmicrotime ();
echo $time _end-$time _start;
?>

[/php]


Stristr ()
First time: 2.74142408371
Second time: 2.52075314522
Third time: 2.52766990662

Strstr ()
First time: 1.43941402435
Second time: 1.44914388657
Third time: 1.51285290718

Strpos ()
First time: 1.42109084129
Second time: 1.40254187584
Third time: 1.38609910011
----------------------------------
As you can see, STRISTR is significantly slower than the other two when judging whether a character (string) exists in another character (string).
Stristr is not sensitive to case
Strstr sensitive to case sensitivity
Strpos cannot determine if there are special characters (including Chinese characters)

Oh, after the use of the time to remember to choose well.

Stristr (), Strstr (), Strpos () speed comparison in PHP

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.