我正在生成关联数组,键值是一个1..n列的字符串连接。
有没有一个最大长度的关键字,将回来咬我?如果是这样的话,我可能会停下来,用不同的方式来做。
发布于 2009-01-21 21:55:08
它似乎只受到脚本内存限制的限制。
经过快速测试,我得到了一个128mb的密钥,没有问题:
ini_set('memory_limit', '1024M');
$key = str_repeat('x', 1024 * 1024 * 128);
$foo = array($key => $key);
echo strlen(key($foo)) . "<br>";
echo strlen($foo[$key]) . "<br>";
发布于 2014-03-26 10:54:18
在zend_hash.h中,您可以找到zend_inline_hash_func()
方法,该方法可以展示如何在PHP中散列密钥字符串,因此使用字符串长度小于8个字符的密钥可以更好地提高性能。
static inline ulong zend_inline_hash_func(char *arKey, uint nKeyLength) {
register ulong hash = 5381;
/* variant with the hash unrolled eight times */
for (; nKeyLength >= 8; nKeyLength -= 8) {
hash = ((hash << 5) + hash) + *arKey++;
hash = ((hash << 5) + hash) + *arKey++;
hash = ((hash << 5) + hash) + *arKey++;
hash = ((hash << 5) + hash) + *arKey++;
hash = ((hash << 5) + hash) + *arKey++;
hash = ((hash << 5) + hash) + *arKey++;
hash = ((hash << 5) + hash) + *arKey++;
hash = ((hash << 5) + hash) + *arKey++;
}
switch (nKeyLength) {
case 7: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
case 6: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
case 5: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
case 4: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
case 3: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
case 2: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
case 1: hash = ((hash << 5) + hash) + *arKey++; break;
case 0: break; EMPTY_SWITCH_DEFAULT_CASE()
}
return hash;
}
https://stackoverflow.com/questions/467149
复制相似问题