PHP中使用最多的非Array莫屬了,那麼Array是如何實現的呢?在PHP內部Array透過一個hashtable來實現,其中使用連結法解決hash衝突的問題,這樣最壞情況下,查找Array元素的複雜度為O(N),最好則為1.
而其計算字串hash值的方法如下,將原始碼摘出來以供查備:
複製程式碼 程式碼如下:
static inline ulong zend_inline_hash_func(const charKeyarKey,nintendic ; 擷取中設定中設定有什麼玄機值?
/* variant with the hash unrolled eight times */
for (; nKeyLength >= 8; nKeyLength -= 8) {
hash = ((hash hash = ((hash hash = ((hash hash = ((hash hash = ((hash hash = ((hash hash = ((hash }
sw (nKeyLengthitch (nKeyLengthitch (nKeyLengthitch) (nKeyLengthitch ) {
case 7: hash = ((hash case 5: hash = ((hash case 4: hash = ((hash case 3: hash = ((hash case 2: hash = ((hash case 1Key; 5( break;
case 0: break;
EMPTY_SWITCH_DEFAULT_CASE()
}
return hash;///返回hash值
}
ps:對於以下函數,仍有兩點不明:
= 53838538385382382375372370000372370003函數,仍有兩點的理由。
這種step=8的循環方式是為了效率麼?