Skip to content

Instantly share code, notes, and snippets.

@rolftimmermans
Created February 18, 2010 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rolftimmermans/307486 to your computer and use it in GitHub Desktop.
Save rolftimmermans/307486 to your computer and use it in GitHub Desktop.
require "benchmark"
range = 1...1000000
n = 100
Benchmark.bmbm do |b|
b.report "r.entries.last" do
n.times do
f = range.entries.last
end
end
b.report "r.entries[-1]" do
n.times do
f = range.to_a[-1]
end
end
b.report "last = nil; r.each { |v| last = v }" do
n.times do
f = nil
range.each { |value| f = value }
end
end
b.report "r.max" do
n.times do
f = range.max
end
end
b.report "r.exclude_end? ? r.last.pred : r.last" do
n.times do
f = range.exclude_end? ? range.last.pred : range.last
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment