Suppose I have some lookup table, q(w, x, y, z)
, where various combination of keys map to different values; i.e., q(0, 0, 0, 0) = a
, q(0, 0, 0, 1) = b
, q(15, 16, 23, "b") = c
.
What's the best way to implement this structure in Ruby in terms of efficiency? The keys will be generated dynamically and will generally be strings. I can think of three different keying methods with hashes:
- Use a string as the key:
q["a, b, c, d"] = 0
- Use a single array as the key:
q[["a", "b", "c", "d"]] = 0
- Use a hash of hashes:
q["a"]["b"]["c"]["d"] = 0
I'm currently using method 2, and it's a little slower than I would like. These key combinations are generated dynamically—if I were to use a hash that takes a single string, will string concatenation be faster? Should I have started with a hash of hashes in the first place? Will this method take more space in memory?