我想知道,根据经验,当我在阵列中有100个关键字时,这两个解决方案中哪一个更快地执行?
谢谢
第一个案例
$string = 'My name is tom';
$array = array("name", "tom");
if(0 < count(array_intersect(explode(' ', strtolower($string)), $array))) {
echo 'found';
}
第二个案例
function in_string ($words, $string)
{
$isFound = false;
foreach ($words as $value) {
$isFound = $isFound || (stripos($string, $value) !== false);
if ($isFound) break;
}
return $isFound;
}
$string = 'My name is tom';
$array = array("name", "tom");
echo in_string($array,$string);
答案 0 :(得分:1)
为什么不写一个简单的5分钟代码,当有大约100行时没有太大区别......
<?php
function in_string ($words, $string)
{
$isFound = false;
foreach ($words as $value) {
$isFound = $isFound || (stripos($string, $value) !== false);
if ($isFound) break;
}
return "#";
}
$start = time();
print("start: " . $start . "<br>");
$c = 0;
while ($c < 10000000) {
$string = 'My name is tom';
$array = array("name", "tom");
if(0 < count(array_intersect(explode(' ', strtolower($string)), $array))) {
echo '#';
}
$c++;
}
$end = time();
print("<br>end: " . $end);
$start = time();
print("<br>start: " . $start . "<br>");
$c = 0;
while ($c < 10000000) {
$string = 'My name is tom';
$array = array("name", "tom");
echo in_string($array,$string);
$c++;
}
$end = time();
print("<br>end: " . $end);
?>
输出:
start: 1476878257
##...
end: 1476878273
start: 1476878273
##...
end: 1476878281
所以做出自己的结论
答案 1 :(得分:1)
自行检查,尝试此代码并添加您的版本:
<?php
function testMe (callable $func, $testName) {
$start = microtime(true);
echo '### START TEST <strong>['.$testName.']</strong> ###<br>';
$i = 0;
while($i < 100000) {
$func();
$i++;
}
echo '### FINSIHED TEST <strong>['.$testName.']</strong> in ['.number_format(( microtime(true) - $start), 4).'sec] ###<br><br>';
}
// my own suggestation
testMe(function() {
$search = ['name', 'tom'];
$string = 'My name is tom';
$content = explode(' ', $string);
$found = array_map(function ($value) use ($string, $content) {
return in_array($value, $content);
}, $search);
}, 'in_array / array_map');
function in_string ($words, $string) {
$isFound = false;
foreach ($words as $value) {
$isFound = $isFound || (stripos($string, $value) !== false);
if ($isFound) break;
}
return $isFound;
}
testMe(function() {
$string = 'My name is tom';
$array = array("name", "tom");
in_string($array,$string);
}, 'in_string');
testMe(function() {
$string = 'My name is tom';
$arr = explode(' ', $string);
$arr = array_flip($arr);
$searchwords = array("name", "tom");
foreach ($searchwords as $key => $word ) {
if( isset($arr[ $word ]) ) {
//echo 'found';
}
}
}, 'foreach/isset');
testMe(function() {
$string = 'My name is tom';
$array = array("name", "tom");
if(0 < count(array_intersect(explode(' ', strtolower($string)), $array))) {
//echo 'found';
}
}, 'array_intersect/explode');
<强>结果:强>
PHP 5.6
### START TEST [in_array / array_map] ###
### FINSIHED TEST [in_array / array_map] in [0.5283sec] ###
### START TEST [in_string] ###
### FINSIHED TEST [in_string] in [0.1444sec] ###
### START TEST [foreach/isset] ###
### FINSIHED TEST [foreach/isset] in [0.2247sec] ###
### START TEST [array_intersect/explode] ###
### FINSIHED TEST [array_intersect/explode] in [0.2923sec] ###
PHP7.0
### START TEST [in_array / array_map] ###
### FINSIHED TEST [in_array / array_map] in [0.1875sec] ###
### START TEST [in_string] ###
### FINSIHED TEST [in_string] in [0.0503sec] ###
### START TEST [foreach/isset] ###
### FINSIHED TEST [foreach/isset] in [0.0940sec] ###
### START TEST [array_intersect/explode] ###
### FINSIHED TEST [array_intersect/explode] in [0.1423sec] ###
可以在http://phptester.net/
答案 2 :(得分:0)
with
这可能需要一些微调以匹配您正在使用的字符串