title: Ruby performance: Multi-key hashes
body: <p>Suppose I have some lookup table, <code>q(w, x, y, z)</code>, where various combination of keys map to different values; i.e., <code>q(0, 0, 0, 0) = a</code>, <code>q(0, 0, 0, 1) = b</code>, <code>q(15, 16, 23, "b") = c</code>. </p>
  
  <p>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:</p>
  
  <ol>
  <li>Use a string as the key: <code>q["a, b, c, d"] = 0</code></li>
  <li>Use a single array as the key: <code>q[["a", "b", "c", "d"]] = 0</code></li>
  <li>Use a hash of hashes: <code>q["a"]["b"]["c"]["d"] = 0</code></li>
  </ol>
  
  <p>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?</p>