對於foreach(在本例中爲php5)數組的「副本」是否具有實際開銷的直接副本,或僅僅是一個懶惰副本(寫入副本),如果它檢測到寫入操作只會產生開銷?php5:foreach的數組的「副本」是否會產生開銷?
另外,在幾個地方的註釋是運行foreach的鍵($ array) - 這怎麼能真的更快?
對於foreach(在本例中爲php5)數組的「副本」是否具有實際開銷的直接副本,或僅僅是一個懶惰副本(寫入副本),如果它檢測到寫入操作只會產生開銷?php5:foreach的數組的「副本」是否會產生開銷?
另外,在幾個地方的註釋是運行foreach的鍵($ array) - 這怎麼能真的更快?
好了,我走了,量了 -
Test This And This And, uh, This
--------------------------- ---------------------------- ----------------------------
Setup Setup Setup
---------------------------- ---------------------------- ----------------------------
$i = array_fill(0,1000,'1'); $j = array_fill(0,1000,'1'); $j = array_fill(0,1000,'1');
$c = 0; $d = 0; $e = 0;
--------------------------- ---------------------------- ---------------------------
Code Under Test Code Under Test Code Under Test
--------------------------- ------------------------------ --------------------------
foreach ($i as $v) foreach (array_keys($j) as $k) foreach ($i as &$v)
{ $c+= $v; } { $d+= $j[$k]; } { $e+= $v; }
--------------------------- ------------------------------ -------------------------
Tear Down Tear Down Tear Down
--------------------------- ----------------------------- -------------------------
print "c = $c"; print "d = $d"; print "e = $e";
---------------------------- ----------------------------- --------------------------
Test for repetitions. 10000
---------------------------- ----------------------------- -------------------------
c = 10000000 d = 10000000 e = 10000000
---------------------------- ----------------------------- -------------------------
Ran in 1.8540189266205 Ran in 4.0039160251617 Ran in 1.9633851051331
--------------------------- ----------------------------- -------------------------
Winner -0.10936617851257 Looser 2.0405309200287 2nd Best 0
--------------------------- ---------------------------- -------------------------
貌似的foreach($ A $爲V)比array_keys好得多,並且使用& v是在中間。
OK,這裏是其他要求的比較(你覺得呢?)
Test This And This
--------------------------------------- --------------------------------------
Setup Setup
-------------------------------------- --------------------------------------
$k = array_fill(0,1000,'1'); $i = array_fill(0,1000,'1');
$f = 0; $c = 0;
$kMax = count($k);
-------------------------------------- --------------------------------------
Code Under Test Code Under Test
-------------------------------------- --------------------------------------
for ($x = 0; $x < kiMax; $x++) foreach ($i as $v)
{ $f+= $k[$x]; } { $c+= $v; }
-------------------------------------- --------------------------------------
Tear Down Tear Down
-------------------------------------- --------------------------------------
print "f = $f"; print "c = $c";
-------------------------------------- --------------------------------------
Test for repetitions: 10,000
-------------------------------------- --------------------------------------
f = 10000000 c = 10000000
-------------------------------------- --------------------------------------
Ran in 2.8563051223755 Ran in 1.8667521476746
-------------------------------------- -------------------------------------
2nd best 0.98955297470093 Winner 0
不錯的一個。我做的基本上是檢查我告訴你的內容,但是代碼不是很漂亮以便粘貼。 我做的另一件事是添加一次性調用以在array_keys版本的循環開始處回顯memory_get_usage()在範圍內確認額外的內存使用情況。使用範圍(1,1000000)我耗盡memory_limit 128M的內存。 順便說一下,你的第三列循環應該是$ e + =,所以你的拆卸不是零。 – scribble 2009-11-17 01:28:24
你在第三個例子中有錯誤: {$ c + = $ v; } 應該是: {$ e + = $ v; } - 這是造成你的壞結果。 – dar7yl 2009-11-17 01:28:27
在這個例子中,* FOR *循環比* FOREACH *循環更快嗎?嘗試一下並告訴我們。 – Ismael 2009-11-17 16:08:25