1樓:張皓旻
foreach在php5裡只需要走一遍鍊錶,php7只要順序遍歷一遍bucket陣列。while迴圈要走hash find
2樓:COACHMANS
就陣列來看:
foreach是在迴圈遍歷的時候,實質上是複製了乙份陣列,就是對陣列副本進行操作的,資料量大的話其實這樣是很慢的,想想都D疼。
while在處理陣列的時候,是直接通過移動陣列的下標也就是陣列鍵值,對當前陣列進行迴圈操作,這種操作方式才是正確合理高效的。
理論上while要比foreach快,但是通過真實測速發現兩者速度還是有差異的同樣讀取100萬條資料foreach比while要快一些。原因就是PHP親生兒子自己內部實現,而while是實實在在的通用迴圈結構式。
<?
php/**
* @author personal
* @desc 使用者類控制器
*/namespace
;use
Yii;
useyii\web\Controller;use
;class
UserController
extends
Controller
/** foreach 100w begin */$fstr=''
;$foreachBegin
=$this
->runMicrotime
();foreach
($arr
as$key
=>$val
)$foreachEnd
=$this
->runMicrotime
();$foreachTime
=$foreachEnd
-$foreachBegin
;echo
'foreachBegin time:'
.$foreachBegin
.' | $foreachEnd time:'
.$foreachEnd
.' | foreach time:'
.$foreachTime.'
';/** foreach 100w end *//** while 100w begin */$wstr=''
;$whileBegin
=$this
->runMicrotime
();$i=0
;$total
=count
($arr
);while($i
<$total
)$whlieEnd
=$this
->runMicrotime
();$whlieTime
=$whlieEnd
-$whileBegin
;echo
'foreachBegin time:'
.$whileBegin
.' | $foreachEnd time:'
.$whlieEnd
.' | foreach time:'
.$whlieTime
;/** while 100w end */}public
function
runMicrotime()}
測試結果
R語言中,如何查詢陣列在陣列 矩陣中的位置?
王帥 indexOf function value,arr indexOf Mono c B Mono CD8 T 2 及年歲之未晏 m matrix c 1,2,4,2,3,5,4,9 ncol 2 m 1 2 1,1 3 2,2 5 3,4 4 4,2 9 y c 2,5 Reduce inte...
求解出國讀數學物理的困境
不是本科生吧,那麼你只要說服你未來的導師就行了。找好導師,看好研究方向,提交材料,就可以了。不知道題主的本科相關課程背景怎麼樣 題主是啥專業啊 如果基本的數學物理課程 物理的話也就量子力學,數學基礎的幾門分析課 有的話可以申請德國Uni Hamburg,Uni Leipzig,LMU都有mathem...
為什麼直接修改陣列長度或設定陣列項的索引時,Vue不能檢測到陣列的變動?
陳小成 vue中的陣列的監聽不是通過Object.defineProperty來實現的,是通過對 push pop shift unshift splice sort reverse 這幾個改變陣列本身的方法執行後來通知監聽達到的,原始碼傳送門 https 井底之蛙 其實說白了就是vue的監聽機制都...