Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add benchmark comparing with Hash and RadixTree

RadixTree is from https://github.com/nahi/radix_tree
You can install it with 'gem install radix_tree'
  • Loading branch information...
commit cad4f5c36b51fc4f83299df7755123506e69a043 1 parent 022c584
Hiroshi Nakamura authored

Showing 1 changed file with 53 additions and 0 deletions. Show diff stats Hide diff stats

  1. +53 0 bench/bench.rb
53 bench/bench.rb
... ... @@ -0,0 +1,53 @@
  1 +require 'benchmark'
  2 +require 'radix_tree' # gem install radix_tree
  3 +require 'avl_tree'
  4 +
  5 +random = Random.new(0)
  6 +
  7 +TIMES = 100000
  8 +key_size = 10
  9 +
  10 +def aset(h, keys)
  11 + TIMES.times do |idx|
  12 + k = keys[idx]
  13 + h[k] = 1
  14 + end
  15 +end
  16 +
  17 +def aref(h, keys)
  18 + TIMES.times do |idx|
  19 + k = keys[idx]
  20 + h[k]
  21 + end
  22 +end
  23 +
  24 +def delete(h, keys)
  25 + TIMES.times do |idx|
  26 + k = keys[idx]
  27 + h.delete(k)
  28 + end
  29 +end
  30 +
  31 +def run(bm, h, keys)
  32 + name = h.class.name
  33 + bm.report("#{name} aset") do
  34 + aset(h, keys)
  35 + end
  36 + bm.report("#{name} aref") do
  37 + aref(h, keys)
  38 + end
  39 + bm.report("#{name} delete") do
  40 + delete(h, keys)
  41 + end
  42 +end
  43 +
  44 +keys = []
  45 +TIMES.times do
  46 + keys << random.bytes(key_size)
  47 +end
  48 +
  49 +Benchmark.bmbm do |bm|
  50 + run(bm, Hash.new, keys)
  51 + run(bm, RadixTree.new, keys)
  52 + run(bm, AVLTree.new, keys)
  53 +end

0 comments on commit cad4f5c

Please sign in to comment.
Something went wrong with that request. Please try again.