带有 if 或条件的执行时间

发布于 2024-12-20 07:19:29 字数 1532 浏览 1 评论 0原文

考虑此类

Class 1

class myclass {
    public static function myfunction($condition, $string)
    {
        if ($condition) {
            // A lot of code here
            // This is just a stupid example
            echo $string;
        }
    }
}

Class 2

class myclass {
    public static function myfunction($condition, $string)
    {
        // A lot of code here
        // This is just a stupid example
        echo $string;
    }
}

和以下文件:

文件 1

myclass::myfunction(($i > 1), '$i is > of 1');
myclass::myfunction(($i > 2), '$i is > of 2');
myclass::myfunction(($i > 3), '$i is > of 3');
myclass::myfunction(($i > 4), '$i is > of 4');
myclass::myfunction(($i > 5), '$i is > of 5');
...
myclass::myfunction(($i > 50), '$i is > of 50'); // this is the amount of functions calls in my project more or less...

文件 2

if ($i > 1) { myclass::myfunction('$i is > of 1'); }
if ($i > 2) { myclass::myfunction('$i is > of 2'); }
if ($i > 3) { myclass::myfunction('$i is > of 3'); }
if ($i > 4) { myclass::myfunction('$i is > of 4'); }
if ($i > 5) { myclass::myfunction('$i is > of 5'); }
...
if ($i > 50) { myclass::myfunction('$i is > of 50'); }

哪个文件运行速度更快(考虑到两个不同的班级)在同一工作基础上? PHP 是否缓存类方法请求,或者只是不断寻找类、方法然后执行它?如果我将条件保留在方法内(因此该方法将被执行),它会改变那么多吗?

Considering this class

Class 1

class myclass {
    public static function myfunction($condition, $string)
    {
        if ($condition) {
            // A lot of code here
            // This is just a stupid example
            echo $string;
        }
    }
}

Class 2

class myclass {
    public static function myfunction($condition, $string)
    {
        // A lot of code here
        // This is just a stupid example
        echo $string;
    }
}

and the following files:

File 1

myclass::myfunction(($i > 1), '$i is > of 1');
myclass::myfunction(($i > 2), '$i is > of 2');
myclass::myfunction(($i > 3), '$i is > of 3');
myclass::myfunction(($i > 4), '$i is > of 4');
myclass::myfunction(($i > 5), '$i is > of 5');
...
myclass::myfunction(($i > 50), '$i is > of 50'); // this is the amount of functions calls in my project more or less...

File 2

if ($i > 1) { myclass::myfunction('$i is > of 1'); }
if ($i > 2) { myclass::myfunction('$i is > of 2'); }
if ($i > 3) { myclass::myfunction('$i is > of 3'); }
if ($i > 4) { myclass::myfunction('$i is > of 4'); }
if ($i > 5) { myclass::myfunction('$i is > of 5'); }
...
if ($i > 50) { myclass::myfunction('$i is > of 50'); }

Which file will run faster (considering both 2 different classes) on the same work base?
Does PHP cache classes method request or just keep looking for the class, the method and then execute it? Does it change that much if I keep the condition inside the method (so the method will be executed)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

执手闯天涯 2024-12-27 07:19:29

我猜测情况 2 在技术上更快,因为您不必传递表达式,并且如果表达式为 false,甚至不会调用该函数(感谢 ajreal!)。即使在最坏的情况下,即表达式始终为 false,第二个也会更快(至少在 C++ 中),除非编译器优化了传递表达式。

然而,理论上它们的运行时间是相同的(BigO 方面),如果您遇到性能问题,这不是它们的原因。换句话说,差异可以忽略不计。过早的优化是万恶之源。

如果您仍然坚持认为这很重要,那么对自己进行基准测试应该不难。尝试使用随机表达式/字符串运行每万次并比较时间差。

I would guess that case 2 is technically faster because you don't have to pass the expression and the function isn't even called if the expression is false (thanks ajreal!). Even in the worst case situation, where the expression is always false, the second would be faster (in C++ at least) unless the compiler optimized passing the expression out.

However, they are both theoretically the same running time (BigO-wise) and if you are having performance issues, this isn't the cause of them. In other words, the difference is negligible. Premature optimization is the root of all evil.

If you still insist on thinking this is significant, it shouldn't be hard to benchmark yourself. Try running each ten thousand times with random expressions/strings and compare the time difference.

如梦初醒的夏天 2024-12-27 07:19:29

第二个更快,因为它不会调用静态方法 50 次(除非 $i 是 50)。
这看起来像是一些设计缺陷,对比较进行分组可能会更适合您。

Second is faster as it does not calling the static method 50 times (unless $i is 50).
This look like some design flaw, grouping the comparison could work better for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文