Skip to content

Instantly share code, notes, and snippets.

@dladowitz
Created October 26, 2012 00:10
Show Gist options
  • Save dladowitz/3956253 to your computer and use it in GitHub Desktop.
Save dladowitz/3956253 to your computer and use it in GitHub Desktop.
Testing timers using Benchmark vs start/end/ diffing
require 'benchmark'
def janky(length)
start_time = Time.now
(1..length).each { |num| num }
time = Time.now - start_time
time
end
def benchmarked(length)
time = Benchmark.realtime do
(1..length).each { |num| num }
end
time
end
puts "janky version time: #{janky(4000)}"
puts "benchmarked version time: #{benchmarked(4000)}"
@dladowitz
Copy link
Author

These two methods apparently aren't the same.

ladowitzs-mbair:Desktop dladowitz$ ruby times.rb
janky version time: 0.000471
benchmarked version time: 0.000594

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment