Heim  >  Artikel  >  Backend-Entwicklung  >  php中比较两个字符串是否相等,==和strcmp( ) 哪个效率高?

php中比较两个字符串是否相等,==和strcmp( ) 哪个效率高?

WBOY
WBOYOriginal
2016-08-18 09:16:312267Durchsuche

php中比较两个字符串是否相等,==和strcmp( ) 哪个效率高?

回复内容:

php中比较两个字符串是否相等,==和strcmp( ) 哪个效率高?

我觉得吧,还是要自己多多实践。

<code><?php $time1 = microtime(true);
$a = $b = 'hello,world!';
$i = $j = 100000000;
while($i>0){
    $i--;
    if($a==$b) ;
}
$time2 = microtime(true);
while($j>0){
    $j--;
    if(strcmp($a,$b));
}
$time3 = microtime(true);

echo ($time2 - $time1)."\n";
echo ($time3 - $time2);

//result
//$ php demo.php
//1.9449820518494
//7.9897949695587
</code>

肯定是== 效率高,因为运算符比函数调用省内存。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn