strstr 또는 strpos가 선호되는 방법은 무엇입니까?
많은 개발자가 strstr과 strpos를 모두 사용하여 하위 문자열이 있는지 확인하고 있음을 알았습니다. 그들 중 하나가 선호되며 그 이유는 무엇입니까?
PHP 온라인 매뉴얼에서 :
특정 바늘이 건초 더미 내에서 발생하는지 확인하려면
strpos()
대신 더 빠르고 메모리 집약적 인 함수를 사용하십시오.
여기 에 몇 가지 다른 답변 (+ 벤치 마크)이 있습니다. 제 질문에 대해 거의 똑같습니다.
그 동안 나는 또한 내가 각 관련 기능에 1000000 번 실행 내 자신의 벤치 마크 테스트를했다 ( strstr()
, strpos()
, stristr()
및 stripos()
).
코드는 다음과 같습니다.
<?php
function getmicrotime() {
list($usec, $sec) = explode(" ", microtime());
return ((float) $usec + (float) $sec);
}
$mystring = 'blahblahblah';
$findme = 'bla';
echo 'strstr & strpos TEST:<pre>';
$time_start = getmicrotime();
for($i=0; $i<1000000; $i++) strstr($mystring, $findme);
$time_needed_strstr = getmicrotime() - $time_start;
echo 'strstr(): ',
round( $time_needed_strstr , 8 ). PHP_EOL;
$time_start = getmicrotime();
for($i=0; $i<1000000; $i++) stristr($mystring, $findme);
$time_needed_stristr = getmicrotime() - $time_start;
echo 'stristr(): ',
round( $time_needed_stristr , 8 ) . PHP_EOL;
$time_start = getmicrotime();
for($i=0; $i<1000000; $i++) strpos($mystring, $findme) !== false;
$time_needed_strpos = getmicrotime() - $time_start;
echo 'strpos() !== false: ',
round( $time_needed_strpos , 8 ) . PHP_EOL;
$time_start = getmicrotime();
for($i=0; $i<1000000; $i++) stripos($mystring, $findme) !== false;
$time_needed_stripos = getmicrotime() - $time_start;
echo 'stripos() !== false: ',
round( $time_needed_stripos , 8 ) . PHP_EOL;
echo PHP_EOL;
echo 'time_needed_stristr - time_needed_strstr: ',
round( $time_needed_stristr - $time_needed_strstr , 8) . PHP_EOL;
echo 'time_needed_stripos - time_needed_strpos: ',
round( $time_needed_stripos - $time_needed_strpos , 8) . PHP_EOL;
echo PHP_EOL;
echo 'time_needed_strstr - time_needed_strpos: ',
round( $time_needed_strstr - $time_needed_strpos , 8) . PHP_EOL;
echo 'time_needed_stristr - time_needed_stripos: ',
round( $time_needed_stristr - $time_needed_stripos , 8) . PHP_EOL;
echo '</pre>';
?>
And here is the first output, which shows that strpos()
is the winner:
strstr & strpos TEST:
strstr(): 2.39144707
stristr(): 3.65685797
strpos() !== false: 2.39055395
stripos() !== false: 3.54681897
time_needed_stristr - time_needed_strstr: 1.2654109
time_needed_stripos - time_needed_strpos: 1.15626502
time_needed_strstr - time_needed_strpos: 0.00089312
time_needed_stristr - time_needed_stripos: 0.110039
The next one is similar to the first output (strpos()
is the winner again):
strstr & strpos TEST:
strstr(): 2.39969015
stristr(): 3.60772395
strpos() !== false: 2.38610101
stripos() !== false: 3.34951186
time_needed_stristr - time_needed_strstr: 1.2080338
time_needed_stripos - time_needed_strpos: 0.96341085
time_needed_strstr - time_needed_strpos: 0.01358914
time_needed_stristr - time_needed_stripos: 0.25821209
Below is another one, which is more interesting, because in this case, strstr()
is the winner:
strstr & strpos TEST:
strstr(): 2.35499191
stristr(): 3.60589004
strpos() !== false: 2.37646604
stripos() !== false: 3.51773095
time_needed_stristr - time_needed_strstr: 1.25089812
time_needed_stripos - time_needed_strpos: 1.14126492
time_needed_strstr - time_needed_strpos: -0.02147412
time_needed_stristr - time_needed_stripos: 0.08815908
This means it can really depend on "environmental circumstances", which are sometimes hard to influence, and can change the result of "micro optimization tasks" like this, in case you are just checking whether a string exists in another one or not.
BUT I think in most cases, strpos()
is the winner in comparison to strstr()
.
I hope this test was useful for someone.
Many developers use strpos
for micro optimization purposes.
Using strstr
also only works if the resulting string cannot be interpreted as false in boolean context.
strpos() detects where in the haystack a particular needle lies. stristr() tests whether the needle is anywhere in the haystack
therefor strpos() is faster and less memory consuming
a reason for strstr(): if your needle is at the beginning of a string, strpos returns 0 (so have to check it with === false)
I prefer strstr()
for readability and easy coding.. strpos() !==false
is confusing a bit..
참고URL : https://stackoverflow.com/questions/5820586/which-method-is-preferred-strstr-or-strpos
'code' 카테고리의 다른 글
C #의 첫 번째 구분자에만 String.Split? (0) | 2020.10.14 |
---|---|
Console.WriteLine이 출력 창에 표시되지 않습니다. (0) | 2020.10.14 |
CodeIgniter의 액티브 레코드로 NOW ()를 데이터베이스에 삽입 (0) | 2020.10.14 |
Jenkins-작업간에 변수 전달? (0) | 2020.10.14 |
파일, 어셈블리 또는 종속성 중 하나를로드 할 수 없습니다. (0) | 2020.10.14 |