首先,我知道在90%的应用程序中,性能差异是完全无关的,但我只需要知道哪种构造更快.还有...
目前网络上关于他们的信息令人困惑.很多人说foreach不好,但从技术上讲,它应该更快,因为它应该简化使用迭代器编写数组遍历的过程.迭代器,也被认为是更快的,但在PHP中也明显非常慢(或者这不是PHP的东西?).我说的是数组函数:next()prev()reset()等等.如果它们是偶数函数,而不是像函数一样的PHP语言特性.
To narrow this down a little:我对以大于1的步长遍历数组不感兴趣(也没有负步长,即.反向迭代).我对往返任意点的遍历也不感兴趣,只对0到长度感兴趣.我也不会经常看到操作超过To narrow this down a little0个键的数组,但是我确实看到一个数组在应用程序的逻辑中被多次遍历!同样对于操作,很大程度上只有字符串操作和 echo .
Here are few reference sites:
http://www.phpbench.com/
http://www.php.lt/benchmark/phpbench.php
我到处都能听到:
-
foreach
较慢,因此for
/while
较快 - PHP
foreach
复制它遍历的数组;要使其更快,您需要使用引用 - code like this:
$key = array_keys($aHash); $size = sizeOf($key);
is faster than a
for ($i=0; $i < $size; $i++)foreach
这是我的问题.我写了这个测试脚本:http://pastebin.com/1ZgK07US,不管我运行这个脚本多少次,我都会得到这样的结果:
foreach 1.1438131332397
foreach (using reference) 1.2919359207153
for 1.4262869358063
foreach (hash table) 1.5696921348572
for (hash table) 2.4778981208801
简而言之:
-
foreach
比foreach
快 -
foreach
比for
快 -
foreach
比for
快 for a hash table
有人能解释一下吗?
- 我做错什么了吗?
- PHP foreach引用真的有意义吗?我的意思是,如果你通过引用,为什么它不会复制它呢?
- foreach语句的等效迭代器代码是什么;我在网上看到过几个,但每次测试它们的时间都很不对劲;我还测试了几个简单的迭代器构造,但似乎从来没有得到过像样的结果--PHP中的数组迭代器真的很糟糕吗?
- 除了FOR/FOREACH(和WHILE)之外,有没有更快的方法/方法/构造来迭代数组?
PHP 5.3.0版
Edit: Answer With help from people here I was able to piece together the answers to all question. I'll summarize them here:
- "我做错什么了吗?" The consensus seems to be: yes, I can't use echo in benchmarks. Personally, I still don't see how echo is some function with random time of execution or how any other function is somehow any different -- that and the ability of that script to just generate the exact same results of foreach better than everything is hard to explain though just "you're using echo" (well what should I have been using). However, I concede the test should be done with something better; though a ideal compromise does not come to mind.
- "PHP foreach引用真的有意义吗?我的意思是,如果你通过引用,为什么它不会复制它呢?" ircmaxell shows that yes it is, further testing seems to prove in most cases reference should be faster -- though given my above snippet of code, most definitely doesn't mean all. I accept the issue is probably too non-intuitive to bother with at such a level and would require something extreme such as decompiling to actually determine which is better for each situation.
- "foreach语句的等效迭代器代码是什么;我在网上看到过几个,但每次测试它们的时间都很不对劲;我还测试了几个简单的迭代器构造,但似乎从来没有得到过像样的结果--PHP中的数组迭代器真的很糟糕吗?" ircmaxell provided the answer bellow; though the code might only be valid for PHP version >= 5
- "除了FOR/FOREACH(和WHILE)之外,有没有更快的方法/方法/构造来迭代数组?" Thanks go to Gordon for the answer. Using new data types in PHP5 should give either a performance boost or memory boost (either of which might be desirable depending on your situation). While speed wise a lot of the new types of array don't seem to be better than array(), the splpriorityqueue and splobjectstorage do seem to be substantially faster. Link provided by Gordon: http://matthewturland.com/2010/05/20/new-spl-features-in-php-5-3/
谢谢所有试图帮忙的人.
对于任何简单的遍历,我可能会坚持使用foreach(非引用版本).