Skip to content

Instantly share code, notes, and snippets.

@tancnle
Created March 12, 2014 22:26
Show Gist options
  • Save tancnle/9517799 to your computer and use it in GitHub Desktop.
Save tancnle/9517799 to your computer and use it in GitHub Desktop.
sort vs sort_by for intensive operation
require 'benchmark'
root_directories = Dir.glob("/*")
n = 5000
Benchmark.bm do |x|
x.report("sort") do
n.times{ root_directories.sort { |a, b| File.ctime(a) <=> File.ctime(b) } }
end
x.report("sort_by") do
n.times{ root_directories.sort_by { |dir| File.ctime(dir) } }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment