Permalink
Browse files

Add elements size benchmark

  • Loading branch information...
nahi committed Jan 13, 2012
1 parent bf82141 commit 1f6c0c5053cf64b2fe75ef205a640331f42027e0
Showing with 55 additions and 6 deletions.
  1. +3 −6 bench/bench.rb
  2. +52 −0 bench/bench_element_size.rb
View
@@ -8,22 +8,19 @@
key_size = 10
def aset(h, keys)
- TIMES.times do |idx|
- k = keys[idx]
+ keys.each do |k|
h[k] = 1
end
end
def aref(h, keys)
- TIMES.times do |idx|
- k = keys[idx]
+ keys.each do |k|
h[k]
end
end
def delete(h, keys)
- TIMES.times do |idx|
- k = keys[idx]
+ keys.each do |k|
h.delete(k)
end
end
@@ -0,0 +1,52 @@
+require 'benchmark'
+require 'radix_tree' # gem install radix_tree
+require 'avl_tree'
+
+random = Random.new(0)
+
+times = 100000
+key_size = 10
+
+def aset(h, keys)
+ keys.each do |k|
+ h[k] = 1
+ end
+end
+
+def aref(h, keys)
+ keys.each do |k|
+ h[k]
+ end
+end
+
+def delete(h, keys)
+ keys.each do |k|
+ h.delete(k)
+ end
+end
+
+def run(bm, h, keys)
+ name = h.class.name
+ bm.report("#{name} aset (#{keys.size})") do
+ aset(h, keys)
+ end
+ bm.report("#{name} aref (#{keys.size})") do
+ aref(h, keys)
+ end
+ bm.report("#{name} delete (#{keys.size})") do
+ delete(h, keys)
+ end
+end
+
+[10000, 20000, 50000, 100000, 200000, 500000].each do |elements|
+ keys = []
+ elements.times do
+ keys << random.bytes(key_size)
+ end
+
+ Benchmark.bm(30) do |bm|
+ run(bm, Hash.new, keys)
+ run(bm, RadixTree.new, keys)
+ run(bm, AVLTree.new, keys)
+ end
+end

0 comments on commit 1f6c0c5

Please sign in to comment.