使用变量与在 PHP 中反复调用相比,性能更好吗?

发布于 2024-12-23 16:52:58 字数 1436 浏览 1 评论 0原文

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

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

发布评论

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

评论(4

庆幸我还是我 2024-12-30 16:52:58

当他明确地将一个问题标记为微优化时,我认为他不会找到一堆“别担心!”答案非常有帮助。 (尽管这个问题在性能优化方面确实非常非常可以忽略不计。)

无论如何,我怀疑分配给变量会更快。如果 PHP 做了某种偷偷摸摸的代码优化,它可能能够被优化掉,但看起来并不是那样的(经过一些非常非常粗略的基准测试之后)。

$array['key'] 需要在哈希表中查找值,而 $val 只是一个普通变量。在哈希表中查找它的开销会导致非常轻微的性能损失,因为必须对密钥进行哈希处理,等等。

(注意:最后一段完全忽略了变量的一般存储方式。我的观点是,这种情况下的数组在变量中包含一个哈希表,而另一个是伪直接访问的变量[在 PHP 中它是无论如何都可以直接访问]。)

When he has explicity tagged a question micro-optimisation, I don't think he will find a bunch of "don't worry about it!" answers very helpful. (Though it is true that this question is very, very negligible in terms of performance optimisation.)

Anyway, I suspected assigned to a variable would be quicker. If PHP did some kind of sneaky code optimisations, it might be able to be optimized out, it does not look like that's what is done (after some very, very, very crude benchmarking).

$array['key'] requires looking up a value in a hash table, whereas $val is just a normal variable. The overhead of looking it up in a hash table causes a very slight performance penalty since the key must be hashed, and so on and so forth.

(Note: that last paragraph completely neglects how variables in general are stored. My point is that arrays in this context contain a hash table within the variable, whereas the other one is a variable that is psuedo-directly accessed [in PHP land it is directly accessed anyway].)

断舍离 2024-12-30 16:52:58

这不是你应该想到的事情。专注于应用设计

基本上,即使我们谈论的是毫秒,哪个性能更好(例如 1 或 2)?

毫秒狩猎在你的情况下没有意义 - 只需编写你可以有效使用的代码

It is not the thing you should ever think of. Focus on application design

Basically which is better (example 1 or 2) for performance, even if we're talking milliseconds?

Milliseconds hunting makes no sense in your case - just write the code you can effectively work with

み青杉依旧 2024-12-30 16:52:58

性能在这里不是问题。代码可读性是。

echo $happiness_level.' something';
echo $happiness_level.' something else';

显然比

echo $_SESSION['happiness_level'].' something';
echo $_SESSION['happiness_level'].' something else';

如果您不担心代码看起来有多好,则可以自由地使用其中一个选项。

Performance is not a problem here. Code readability is.

echo $happiness_level.' something';
echo $happiness_level.' something else';

is clearly more readable than

echo $_SESSION['happiness_level'].' something';
echo $_SESSION['happiness_level'].' something else';

If you're not worried about how good your code looks, you're free to use either one of the options.

A君 2024-12-30 16:52:58

我会尝试通过围绕两个版本运行循环来对其进行基准测试。在运行每个循环之前启动一个计时器,并在循环完成时停止它。无论哪一个时间最短,性能都更好。

我敢打赌他们俩几乎是一样的。

I would try to benchmark it by running a loop around both versions. Have a timer start before running each loop and stop it when the loop is done. Whicherver has the shortest time is better for performance.

My bet is on them both being just about the same.

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