问题描述
我已阅读,这是更快通过散列迭代不是通过一个数组。从散列检索值也快多了。
I have read that it is much faster to iterate through a hash than through an array. Retrieving values from a hash is also much faster.
而不是使用一个数组,为什么不使用哈希,并给每个键对应的索引值?如果项永远需要处于顺序,它们可以被排序。
Instead of using an array, why not just use a hash and give each key a value corresponding to an index? If the items ever need to be in order, they can be sorted.
推荐答案
从哈希检索中,你可以通过直接键获取,而不是遍历整个哈希(或数组价值感快,当你正在寻找特定字符串)。有了这样的说, $ {哈希键}
不大于 $阵列更快[0]
因为没有迭代正在的地方。
Retrieving from hash is faster in a sense that you can fetch value directly by key instead of iterating over whole hash (or array when you're searching for particular string). Having that said, $hash{key}
isn't faster than $array[0]
as no iteration is taking place.
阵列无法通过哈希来代替,因为它们各有特点,
Arrays can't be replaced by hashes, as they have different features,
arrays hashes
------------------------------------
ordered keys x -
push/pop x -
suitable for looping x -
named keys - x
这篇关于究竟为什么使用数组,而不是一个哈希?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!