Skip to content

Instantly share code, notes, and snippets.

@nickserv
Last active December 9, 2015 19:18
Show Gist options
  • Save nickserv/4316036 to your computer and use it in GitHub Desktop.
Save nickserv/4316036 to your computer and use it in GitHub Desktop.
Example: Ruby Benchmark
require 'benchmark'
Benchmark.bm do |b|
b.report "summation equation" do
sum = 1_000_000*(1_000_000+1)/2.to_f
end
b.report "each loop" do
sum = 0
(1..1_000_000).each { |n| sum += n }
end
b.report "for loop" do
sum = 0
for n in 1..1_000_000
sum += n
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment