Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

clean up benchmarks

  • Loading branch information...
commit f3f7d4aa581d05f0381e4b5a770dff47d66503cf 1 parent 301c092
@kanwei authored
Showing with 53 additions and 51 deletions.
  1. +0 −51 benchmark.rb
  2. +17 −0 benchmarks/deque.rb
  3. +36 −0 benchmarks/treemaps.rb
View
51 benchmark.rb
@@ -1,51 +0,0 @@
-# :nodoc: all
-$: << File.join(File.expand_path(File.dirname(__FILE__)), 'lib')
-require 'algorithms'
-
-require 'benchmark'
-include Benchmark
-
-# Benchmark heap
-@random_array = []
-@num_items = 10000
-@num_items.times { |x| @random_array << rand(@num_items).to_s }
-# @heap = Containers::MaxHeap.new(@random_array)
-#
-# benchmark do |bench|
-# bench.report("Array#max:\t ") do
-# @num_items.times { @random_array.delete_at(@random_array.index(@random_array.max)) }
-# end
-# bench.report("MaxHeap:\t ") do
-# @num_items.times { @heap.max! }
-# end
-# end
-
-# Benchmark Deque
-deque = Containers::Deque.new
-array = []
-benchmark do |bench|
- bench.report("Array:\t ") { 1000000.times { |x| array << x } }
- bench.report("Deque:\t ") { 1000000.times { |x| deque.push_back(x) } }
-end
-
-# Benchmark Search trees
-@rb_tree = Containers::RBTreeMap.new
-@splay_tree = Containers::SplayTreeMap.new
-@hash = Hash.new
-
-benchmark do |bench|
- puts "\nInsertion"
- bench.report("RBTree: \t") { @random_array.each_with_index { |x,index| @rb_tree[index] = x } }
- bench.report("SplayTree: \t") { @random_array.each_with_index { |x,index| @splay_tree[index] = x } }
- bench.report("Hash: \t\t") { @random_array.each_with_index { |x,index| @hash[index] = x } }
-
- puts "\nTest has_key?"
- bench.report("RBTree: \t") { @num_items.times { |n| @rb_tree.has_key?(n) } }
- bench.report("SplayTree: \t") { @num_items.times { |n| @splay_tree.has_key?(n) } }
- bench.report("Hash: \t\t") { @num_items.times { |n| @hash.has_key?(n) } }
-
- puts "\nSorted order"
- bench.report("RBTree: \t") { @rb_tree.each { |k, v| k } }
- bench.report("SplayTree: \t") { @splay_tree.each { |k, v| k } }
- bench.report("Hash: \t\t") { @hash.sort.each { |k, v| k } }
-end
View
17 benchmarks/deque.rb
@@ -0,0 +1,17 @@
+$: << File.join(File.expand_path(File.dirname(__FILE__)), '../lib')
+require 'algorithms'
+include Algorithms
+
+require 'rubygems'
+require 'rbench'
+
+RBench.run(2) do
+ %w(array deque).each { |s| self.send(:column, s.intern) }
+ deque = Containers::Deque.new
+ array = []
+
+ report "Insertion at end" do
+ array { 1000000.times { |x| array << x } }
+ deque { 1000000.times { |x| deque.push_back(x) } }
+ end
+end
View
36 benchmarks/treemaps.rb
@@ -0,0 +1,36 @@
+$: << File.join(File.expand_path(File.dirname(__FILE__)), '../lib')
+require 'algorithms'
+include Algorithms
+
+require 'rubygems'
+require 'rbench'
+
+RBench.run(10) do
+ trees = %w(hash rbtree splaytree)
+ trees.each { |tree| self.send(:column, tree.intern) }
+
+ rbtree = Containers::RBTreeMap.new
+ splaytree = Containers::SplayTreeMap.new
+ hash = Hash.new
+
+ random_array = Array.new(10000) { |i| rand(i) }
+ num = 1000
+
+ report "Insertion" do
+ rbtree { random_array.each_with_index { |x,index| rbtree[index] = x } }
+ splaytree { random_array.each_with_index { |x,index| splaytree[index] = x } }
+ hash { random_array.each_with_index { |x,index| hash[index] = x } }
+ end
+
+ report "has_key?" do
+ rbtree { num.times { |n| rbtree.has_key?(n) } }
+ splaytree { num.times { |n| splaytree.has_key?(n) } }
+ hash { num.times { |n| hash.has_key?(n) } }
+ end
+
+ report "Lookup in sorted order" do
+ rbtree { rbtree.each { |k, v| k } }
+ splaytree { splaytree.each { |k, v| k } }
+ hash { hash.sort.each { |k, v| k } }
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.