在另一个字符串中查找某个字符串出现的最快方法是什么? [英] What is the fastest way to find the occurrence of a string in another string?

查看:115
本文介绍了在另一个字符串中查找某个字符串出现的最快方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Possible Duplicate:
Which method is preferred strstr or strpos ?

嗨!

您能告诉我哪个更快?:
strstr($mystring, $findme);
OR
strpos($mystring, $findme);

其他

在另一个字符串中找到字符串的第一个或任何一个?

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?

如果我使用 stripos() ?

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 which 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.

我已经在各种文章中找到一些有关速度差异的评论(例如, php.net ,有人说strpos之后如果有!== false检查,则strstr()更快),但现在我无法确定哪个是真的.

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!

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

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



$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似乎处于领先地位,我已经通过在'The quick brown fox jumps over the lazy dog'中找到一些字符串对它进行了测试:

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

?>

这篇关于在另一个字符串中查找某个字符串出现的最快方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆