当开发人员使用strpos检查子字符串是否存在时,我只见过开发人员使用严格比较:
if (strpos($haystack,$needle) !== false) {
}今天我突然想到可以使用is_numeric ins。
if (is_numeric(strpos($haystack,$needle))) {
}是否有一个人会使用一个而另一个(特别是在这个用例中)的原因?
考虑一下,strpos的目的是返回子字符串的位置。只有当它不存在时,它才会返回假的。这个职位是个数字。因此,is_numeric很有资格被认为是语义上的。
发布于 2019-02-16 10:32:58
我发现这个问题搜索的问题几乎相同,除了我的想法是使用is_int( )而不是is_numeric( )。我无法真正理解发布的基准,所以我做了自己的。简而言之,is_numeric( )的头发比!== false慢,is_int( )的头发更快。就我个人而言,我发现它比!== false更易读,而不是更少,所以我可能会切换到它。
这是在php v7.1.23下完成的。
结果:
0.7900s for '!== false' with hits
4.5137s for '!== false' with misses
0.9297s for 'is_numeric' with hits
4.7509s for 'is_numeric' with misses
0.6391s for 'is_int' with hits
4.4862s for 'is_int' with misses根据守则:
$n_times = 10000000;
$haystack_hit = " Certificate Name: example.com";
$heystack_miss = "Found the following certs:";
$needle = "Certificate Name:";
$start = microtime(true);
for($i = 0; $i < $n_times; $i++)
{
if (strpos($haystack_hit,$needle) !== false)
{ }
}
$end = microtime(true) - $start;
echo "\n" . round($end,4) . "s for '!== false' with hits\n";
$start = microtime(true);
for($i = 0; $i < $n_times; $i++)
{
if (strpos($haystack_miss,$needle) !== false)
{ }
}
$end = microtime(true) - $start;
echo round($end,4) . "s for '!== false' with misses\n\n";
// -----
$start = microtime(true);
for($i = 0; $i < $n_times; $i++)
{
if ( is_numeric(strpos($haystack_hit,$needle)) )
{ }
}
$end = microtime(true) - $start;
echo round($end,4) . "s for 'is_numeric' with hits\n";
$start = microtime(true);
for($i = 0; $i < $n_times; $i++)
{
if ( is_numeric(strpos($haystack_miss,$needle)) )
{ }
}
$end = microtime(true) - $start;
echo round($end,4) . "s for 'is_numeric' with misses\n\n";
// -----
$start = microtime(true);
for($i = 0; $i < $n_times; $i++)
{
if ( is_int(strpos($haystack_hit,$needle)) )
{ }
}
$end = microtime(true) - $start;
echo round($end,4) . "s for 'is_int' with hits\n";
$start = microtime(true);
for($i = 0; $i < $n_times; $i++)
{
if ( is_int(strpos($haystack_miss,$needle)) )
{ }
}
$end = microtime(true) - $start;
echo round($end,4) . "s for 'is_int' with misses\n";发布于 2016-01-15 02:06:10
我创造了一个基准。Case check by compare with false value总是比check by is_numeric快。
// Init a big string array
for($i = 0; $i < 10000; $i++){
$str[] = 'a'.rand().'b'.rand();
}
// Case comparing with false value
$time1 = microtime(true);
foreach($str as $st){
$res[] = strpos($st,rand(0, count(array('b', 'c')) - 1 )) !== false;
}
$time2 = microtime(true);
echo $time2-$time1.'<br/>';
// Case 'is_numeric'
$time3 = microtime(true);
foreach($str as $st){
$res[] = is_numeric(strpos($st,rand(0, count(array('b', 'c')) - 1 )));
}
$time4 = microtime(true);
echo $time4-$time3;
//Time 1:
//0.018877029418945
//0.020556926727295
//Time 2:
//0.016352891921997
//0.016934871673584
//Time 3:
//0.0121009349823
//0.01330304145813
//Time 4:
//0.017507076263428
//0.01904296875发布于 2016-01-15 01:26:54
由于strpos返回整数或布尔值false,所以可以使用is_numeric。
问题是:
更重要的是,使用autodocumented/self-descriptive,或将返回值与布尔值进行比较,惯用的和is_numeric是什么?与布尔假相比,IMO要直观得多:
$string = 'new object';
$found = strpos($string,'new');
echo (is_numeric($found)) ? 'found' : 'not found';
echo "\n";
# much better
echo ($found !== false) ? 'found' : 'not found';
echo "\n";此外,strpos(...) !== false被过度使用,因为它是PHP文档所建议的。因此,它已成为惯例。
https://stackoverflow.com/questions/34802829
复制相似问题