Skip to content

Instantly share code, notes, and snippets.

@jensendarren
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jensendarren/bc605c714f71f549180a to your computer and use it in GitHub Desktop.
Save jensendarren/bc605c714f71f549180a to your computer and use it in GitHub Desktop.
An example of using Benchmark lib in Ruby
require 'benchmark'
# Compare sorting a Hash with an Array
arr = []
hash = {}
10_000.times.with_index do |i|
ascii_code = rand(255)
random_char = ascii_code.chr
arr << random_char
hash[i] = random_char
end
# puts arr.inspect
#puts hash.inspect
#sh=[]
Benchmark.bm(7) do |x|
x.report 'Sort Array' do
arr.sort!
end
x.report 'Sort Hash' do
sh = hash.sort_by{|_key, value| value}
end
end
# puts arr.inspect
# puts sh.inspect
# puts hash.inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment