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].)
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.
发布评论
评论(4)
当他明确地将一个问题标记为微优化时,我认为他不会找到一堆“别担心!”答案非常有帮助。 (尽管这个问题在性能优化方面确实非常非常可以忽略不计。)
无论如何,我怀疑分配给变量会更快。如果 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].)
这不是你应该想到的事情。专注于应用设计
毫秒狩猎在你的情况下没有意义 - 只需编写你可以有效使用的代码
It is not the thing you should ever think of. Focus on application design
Milliseconds hunting makes no sense in your case - just write the code you can effectively work with
性能在这里不是问题。代码可读性是。
显然比
如果您不担心代码看起来有多好,则可以自由地使用其中一个选项。
Performance is not a problem here. Code readability is.
is clearly more readable than
If you're not worried about how good your code looks, you're free to use either one of the options.
我会尝试通过围绕两个版本运行循环来对其进行基准测试。在运行每个循环之前启动一个计时器,并在循环完成时停止它。无论哪一个时间最短,性能都更好。
我敢打赌他们俩几乎是一样的。
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.