Created
February 18, 2009 18:15
-
-
Save stevej/66455 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 | |
# | |
# Testing whether sorting then reversing is faster than sorting "the right way". | |
# Short answer: sort/reverse are written in C, your own sort function will be written in Ruby. | |
require 'benchmark' | |
n = 1_000 | |
ids = (0..10_000).to_a.sort_by { rand } | |
# user system total real | |
# sort.reverse 2.510000 0.150000 2.660000 ( 2.904603) | |
Benchmark.bm do |x| | |
x.report("sort.reverse") { n.times do ; ids.sort.reverse ; end } | |
end | |
# user system total real | |
# sort method 81.090000 0.790000 81.880000 ( 85.358657) | |
Benchmark.bm do |x| | |
x.report("sort method") { n.times do ; ids.sort {|a,b| b <=> a} ; end } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment