Browse files

Benchmarks for Sort algorithms

  • Loading branch information...
1 parent adb0ae4 commit 71073139326567e4f9d64cf96b79372e363e4f64 @kanwei committed Sep 16, 2008
Showing with 34 additions and 0 deletions.
  1. +34 −0 benchmarks/sorts.rb
View
34 benchmarks/sorts.rb
@@ -0,0 +1,34 @@
+$: << File.join(File.expand_path(File.dirname(__FILE__)), '../lib')
+require 'algorithms'
+include Algorithms
+
+require 'rubygems'
+require 'rbench'
+
+RBench.run(5) do
+
+ sorts = %w(ruby comb_sort heapsort insertion_sort shell_sort quicksort mergesort)
+ sorts.each { |sort| self.send(:column, sort.intern) }
+
+ n = 1000
+
+ proc = lambda { |scope, ary|
+ scope.ruby { ary.dup.sort }
+ scope.comb_sort { Sort.comb_sort(ary.dup) }
+ scope.heapsort { Sort.heapsort(ary.dup) }
+ scope.insertion_sort { Sort.insertion_sort(ary.dup) }
+ scope.shell_sort { Sort.shell_sort(ary.dup) }
+ scope.quicksort { Sort.quicksort(ary.dup) }
+ scope.mergesort { Sort.mergesort(ary.dup) }
+ }
+
+ report "Already sorted" do
+ sorted_array = Array.new(n) { rand(n) }.sort
+ proc.call(self, sorted_array)
+ end
+
+ report "Random" do
+ random_array = Array.new(n) { rand(n) }
+ proc.call(self, random_array)
+ end
+end

0 comments on commit 7107313

Please sign in to comment.