Created
November 28, 2016 23:54
-
-
Save hrp/a795f6db1d32083064571910ce702ee7 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/ruby | |
require 'benchmark' | |
puts "Running Ruby #{RUBY_VERSION}" | |
ary = [] | |
200.times { | |
ary << {:bar => Time.at(rand * Time.now.to_i)} | |
} | |
n = 1000 | |
puts "n=#{n}" | |
Benchmark.bm(20) do |x| | |
x.report("sort") { n.times { ary.dup.sort{ |a,b| b[:bar] <=> a[:bar] } } } | |
x.report("sort reverse") { n.times { ary.dup.sort{ |a,b| a[:bar] <=> b[:bar] }.reverse } } | |
x.report("sort_by Time-a[:bar]") { n.times { ary.dup.sort_by{ |a| Time.now - a[:bar] } } } | |
x.report("sort_by Time-a[:bar]") { n.times { ary.dup.sort_by{ |a| Time.now - a[:bar].to_i } } } | |
x.report("sort_by 0-a[:bar]") { n.times { ary.dup.sort_by{ |a| -a[:bar].to_i } } } | |
x.report("sort_by a[:bar]*-1") { n.times { ary.dup.sort_by{ |a| a[:bar].to_i*-1 } } } | |
x.report("sort_by.reverse") { n.times { ary.dup.sort_by{ |a| a[:bar] }.reverse } } | |
x.report("sort_by.reverse!") { n.times { ary.dup.sort_by{ |a| a[:bar] }.reverse! } } | |
end |
Author
hrp
commented
Nov 28, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment