问题
I would like to know how an associative array is constructed/implemented when used in shell scripts.
Also, I want to know if the time complexity of shell script based associative arrays are optimal since we can use alphabets as well as numbers as their respective keys.
EDIT: what hash function do they use??
回答1:
If you're using an associative array, you're not accessing it via "use alphabets as well as numbers as their respective keys"; You're using strings - any numbers are the character representations, not an actual index.
I can't find anything concrete short of looking through the source, but by most accounts it appears that internally it's implemented as a hash table (rather than a tree) so your access and insert average time is going to be O(1). Doesn't get much more optimal.
来源:https://stackoverflow.com/questions/5710043/time-complexity-of-associative-arrays-in-shell-scripts