Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.