Last active
August 29, 2015 14:13
-
-
Save jensendarren/bc605c714f71f549180a to your computer and use it in GitHub Desktop.
An example of using Benchmark lib in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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