一応、アクセス速度も測ってみる。
require 'sparsehash' require 'timeout' include Sparsehash [100, 10000, 1000000].each do |rnum| [Hash, DenseHashMap].each do |clazz| puts("#{clazz}: #{rnum}") hash = clazz.new (0...rnum).each do |i| buf = sprintf("%08d", i) hash[buf] = buf end begin timeout(300) do time = Time.now (0...rnum).each do |i| buf = sprintf("%08d", i) buf = hash[buf] end time = Time.now - time printf("Time: %.3f sec.\n\n", time) end rescue Timeout::Error printf("-----\n\n") end end end
rnum = | 100 | 10000 | 1000000 |
---|---|---|---|
Hash | 0.000 sec. | 0.046 sec. | 19.469 sec. |
DenseHashMap | 0.000 sec. | 0.157 sec. | 82.031 sec. |
1000000だと結構、時間食うなー。