在另一个字符串中查找字符串出现的最快方法是什么?

Sk8*_*ter 8 php string substring

可能重复:
哪种方法首选strstr或strpos?

嗨!

你能告诉我哪一个是速度更快:OR 或 其他任何在寻找-第一或任何-字符串出现在另一个呢?
strstr($mystring, $findme);

strpos($mystring, $findme);


如果我使用stristr()OR 检查不区分大小写模式中的事件,它在性能方面是否重要stripos()

在我的情况下,给定字符串在哪个确切位置(如果有),或者在另一个字符串中发生了多少次(如果有的话)并不重要,唯一重要的问题是它是否存在于另一个字符串中.

我已经在各种文章中发现了一些关于速度差异的评论(例如在php.net上,有人说strstr()在strpos之后有一个!==错误检查更快),但现在我无法决定哪个是真的.

如果你知道在另一个方法中搜索字符串的更好方法,请告诉我!

非常感谢您的相关评论!

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

一个例子:


$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';  
}  


pin*_*txo 22

strpos似乎是领先的,我已经通过找到一些字符串来测试它'The quick brown fox jumps over the lazy dog':

  • strstr 使用0.48487210273743秒进行1000000次迭代查找 'quick'
  • strpos 使用0.40836095809937秒进行1000000次迭代查找 'quick'
  • strstr 使用0.45261287689209秒进行1000000次迭代查找 'dog'
  • strpos 使用0.39890813827515秒进行1000000次迭代查找 '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'";

?>
Run Code Online (Sandbox Code Playgroud)


Jas*_*ary 15

来自PHP文档:

注意:

如果您只想确定特定针是否出现在haystack中,请使用更快且内存更少的内存密集型函数strpos().

我愿意接受他们的话:)

  • @Matthieu,我认为你很滑稽. (4认同)

Ali*_*xel 6

更快的方法是:

if (strpos($haystack, $needle) !== false)
Run Code Online (Sandbox Code Playgroud)

不区分大小写的版本应该明显更慢(至少慢2倍,我期待).


strncmp()/ substr()都不可能有更好的表现当且仅当你检查是否$haystack 开始$needle,如果$haystack是相当长的(>数百个字符左右).


基准测试:

请参阅其他基准@ http://net-beta.net/ubench/(搜索strpos).


一种实际的例子,这种优化(种类)很重要 - 计算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;
Run Code Online (Sandbox Code Playgroud)