编辑::哦,我忘了
class Test1{
public static function test(){
for($i=0; $i<=1000; $i++)
$j += $i;
}
}
class Test2{
public function test() {
for ($i=0; $i<=1000; $i++){
$j += $i;
}
}
}
Run Code Online (Sandbox Code Playgroud)
对于这个算法
$time_start = microtime();
$test1 = new Test2();
for($i=0; $i<=100;$i++)
$test1->test();
$time_end = microtime();
$time1 = $time_end - $time_start;
$time_start = microtime();
for($i=0; $i<=100;$i++)
Test1::test();
$time_end = microtime();
$time2 = $time_end - $time_start;
$time = $time1 - $time2;
echo "Difference: $time";
Run Code Online (Sandbox Code Playgroud)
我有结果
Difference: 0.007561
Run Code Online (Sandbox Code Playgroud)
而这些天,我试图让我的方法尽可能静态.但它真的是真的,至少对于PHP来说
我对客观分析感兴趣,哪个性能更高; 调用单例类的实例方法或静态类的方法.我已经看过这个,所以我不是在寻找关于两者之间差异的讨论,或者讨论哪个是"更好".我只对两者之间的相对表现感兴趣.提前致谢.
-麦克风