function test($matches)
{
$fx = '';
if(strpos($matches[0],"http://")===false)
{
$fx = "http://";
}
elseif(strpos($matches[0],"http:/")===false)
{
$fx = "http:/";
}
elseif(strpos($matches[0],"http:")===false)
{
$fx = "http:";
}
elseif(strpos($matches[0],"http")===false)
{
$fx = "http";
}
return $fx.$matches[0];
}
或
function test($matches)
{
if(strpos($matches[0],"http://")===false)
{
return "http://".$matches[0];
}
elseif(strpos($matches[0],"http:/")===false)
{
return "http:/".$matches[0];
}
elseif(strpos($matches[0],"http:")===false)
{
return "http:".$matches[0];
}
elseif(strpos($matches[0],"http")===false)
{
return "http".$matches[0];
}
}
我正在尝试编写我的脚本给他们使用尽可能少的内存,但idk有时代码看起来很丑,在我的opnion中,第一个脚本看起来更漂亮和有条理,但我认为它使用了更多的服务器内存。
谢谢。
至于哪一个会使用更少的内存,我认为它们是等效的(在最坏的情况下都是)。
但是,至于它们的运行时间,第二个将比第一个快几分之一纳秒完成执行,因为一旦找到一个匹配的条件,它就会返回。总的来说,Big-O的差异是恒定的,我们都知道这不会产生差异:O(3n)在最好的情况下与O(n)相对,在最坏的情况下,O(3n)与O(3n)的对比O(3n)(取决于$match[0]的大小)。但这一切都深深依赖于 strpos() 函数的复杂性。
为了可读性,它们也是平等的编写和结构。