Last active
August 29, 2015 14:09
-
-
Save libitte/e4c05c3e9154d2f1fe7b to your computer and use it in GitHub Desktop.
This file contains hidden or 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/env ruby | |
require 'benchmark' | |
ary = [] | |
1000.times { | |
ary << {:bar => rand(1000)} | |
} | |
n = 500 | |
Benchmark.bm(20) do |x| | |
x.report("sort") { n.times { ary.sort{ |a,b| b[:bar] <=> a[:bar] } } } | |
x.report("sort reverse") { n.times { ary.sort{ |a,b| a[:bar] <=> b[:bar] }.reverse } } | |
x.report("sort_by -a[:bar]") { n.times { ary.sort_by{ |a| -a[:bar] } } } | |
x.report("sort_by a[:bar]*-1") { n.times { ary.sort_by{ |a| a[:bar]*-1 } } } | |
x.report("sort_by.reverse!") { n.times { ary.sort_by{ |a| a[:bar] }.reverse } } | |
end | |
# user system total real | |
#sort 0.530000 0.000000 0.530000 ( 0.539872) | |
#sort reverse 0.550000 0.000000 0.550000 ( 0.551648) | |
#sort_by -a[:bar] 0.240000 0.010000 0.250000 ( 0.239292) | |
#sort_by a[:bar]*-1 0.230000 0.000000 0.230000 ( 0.230042) | |
#sort_by.reverse! 0.220000 0.000000 0.220000 ( 0.226368) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment