开发者

What is the fastest way to find the occurrence of a string in another string? [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Which method is preferred strstr or strpos ?

Hi!

Could you tell me which one is faster:

strstr($mystring, $findme);

OR

strpos($mystring, $findme);

OR

anything else

in finding the - first or any - occurrence of a string in another one?

Does it even matter in performance if I check the occurrence in a case-insensitive mode with stristr() OR stripos()?

In my case it doesn't matter in w开发者_运维问答hich exact position the given string is (if any), or how many times it occurs in the other one (if any), the only important question is if it even exists in the other string.

I've already found some comments about differences of speed in various articles (e.g. on php.net, someone says strstr() is faster in case there is a !== false check after strpos), but now I can't decide which is true.

If you know about any better methods of searching a string in another, please let me know!

Thank you very much for the relevant comments!

============

An example:


$mystring = 'blahblahblah';  
$findme = 'bla';  

if(strstr($mystring, $findme)){  
   echo 'got it';  
}  
else{  
   echo 'none';  
}  

echo PHP_EOL;

if(strpos($mystring, $findme) !== false){  
   echo 'got it';  
}  
else{  
   echo 'none';  
}  



strpos seems to be in the lead, I've tested it with finding some strings in 'The quick brown fox jumps over the lazy dog':

  • strstr used 0.48487210273743 seconds for 1000000 iterations finding 'quick'
  • strpos used 0.40836095809937 seconds for 1000000 iterations finding 'quick'
  • strstr used 0.45261287689209 seconds for 1000000 iterations finding 'dog'
  • strpos used 0.39890813827515 seconds for 1000000 iterations finding 'dog'
<?php

    $haystack = 'The quick brown fox jumps over the lazy dog';

    $needle = 'quick';

    $iter = 1000000;

    $start = microtime(true);
    for ($i = 0; $i < $iter; $i++) {
        strstr($haystack, $needle);
    }
    $duration = microtime(true) - $start;
    echo "<br/>strstr used $duration microseconds for $iter iterations finding 'quick' in 'The quick brown fox jumps over the lazy dog'";

    $start = microtime(true);
    for ($i = 0; $i < $iter; $i++) {
        strpos($haystack, $needle);
    }
    $duration = microtime(true) - $start;
    echo "<br/>strpos used $duration microseconds for $iter iterations finding 'quick' in 'The quick brown fox jumps over the lazy dog'";

    $needle = 'dog';

    $start = microtime(true);
    for ($i = 0; $i < $iter; $i++) {
        strstr($haystack, $needle);
    }
    $duration = microtime(true) - $start;
    echo "<br/>strstr used $duration microseconds for $iter iterations finding 'dog' in 'The quick brown fox jumps over the lazy dog'";

    $start = microtime(true);
    for ($i = 0; $i < $iter; $i++) {
        strpos($haystack, $needle);
    }
    $duration = microtime(true) - $start;
    echo "<br/>strpos used $duration microseconds for $iter iterations finding 'dog' in 'The quick brown fox jumps over the lazy dog'";

?>


From the PHP Docs:

Note:

If you only want to determine if a particular needle occurs within haystack, use the faster and less memory intensive function strpos() instead.

I'm willing to take their word for it :)


The faster way is:

if (strpos($haystack, $needle) !== false)

The case insensitive versions should obviouslly be slower (at least 2x slower, I expect).


strncmp() / substr() can possibly perform better iff you're checking if $haystack starts with $needle and if $haystack is considerably long (> hundreds chars or so).


Benchmark:

  • strpos() vs. strncmp() = short | long

See other benchmarks @ http://net-beta.net/ubench/ (search for strpos).


A pratical example where this kind of optimizations (kind of) do matter - calculating hashcashes:

$count = 0;
$hashcash = sprintf('1:20:%u:%s::%u:', date('ymd'), $to, mt_rand());

while (strncmp('00000', sha1($hashcash . $count), 5) !== 0)
{
    ++$count;
}

$header['X-Hashcash'] = $hashcash . $count;


According to the php manpages, strpos is faster and less memory intensive than strstr.


Trick question. They do two different things. One returns the substring, the other returns the starting position of the substring withing the string. The real answer is you are comparing apples to oranges, use which one you need.


If the string A against which you want to find an occurrence of a pattern B, then the fastest way is to build a Suffix Tree of A and perform against it searches for B.


I would think that strpos() would be faster because it only returns an integer (or false if no match was found). strstr() returns a string which contains all text after and including the first match.

For case insensitive searches, I would think that these would be slightly slower because they have to perform extra checks ("do the two chars match? if no, is the char a letter? if yes, does it match the lowercase version? if no, does it match the upper case version?", etc)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜