Skip to content

Instantly share code, notes, and snippets.

@arktisklada
Created May 19, 2015 19:30
Show Gist options
  • Save arktisklada/895d0fd71c57b74174fc to your computer and use it in GitHub Desktop.
Save arktisklada/895d0fd71c57b74174fc to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
require 'benchmark'
puts "Running Ruby #{RUBY_VERSION}"
ary = []
1000.times {
ary << {
char: (65 + rand(26)).chr,
percent: rand(100)
}
}
n = 500
puts "n=#{n}"
Benchmark.bm(20) do |x|
x.report("sort") {n.times {ary.dup.sort{|a,b| [a[:percent], b[:char]] <=> [b[:percent], a[:char]]}}}
x.report("sort reverse") {n.times {ary.dup.sort{|a,b| [a[:percent], b[:char]] <=> [b[:percent], a[:char]]}.reverse}}
x.report("sort -1") {n.times {ary.dup.sort{|a,b| ([a[:percent], b[:char]] <=> [b[:percent], a[:char]]) * -1}}}
x.report("sort! -1") {n.times {ary.dup.sort!{|a,b| ([a[:percent], b[:char]] <=> [b[:percent], a[:char]]) * -1}}}
x.report("sort! reverse!") {n.times {ary.dup.sort!{|a,b| [a[:percent], b[:char]] <=> [b[:percent], a[:char]]}.reverse!}}
end
@katherinewallace
Copy link

If anything my way is coming out worse when I run it, haha...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment