Skip to content

Instantly share code, notes, and snippets.

@kenchan kenchan/compare.rb
Created Sep 6, 2018

Embed
What would you like to do?
require 'benchmark/ips'
Benchmark.ips do |x|
x.report("if") do |i|
if i % 5 == 0
i
end
end
x.report("raise") do |i|
begin
raise unless i % 5 == 0
rescue
i
end
end
x.compare!
end
Warming up --------------------------------------
if 319.284k i/100ms
raise 69.463k i/100ms
Calculating -------------------------------------
if 317.735B (± 6.4%) i/s - 831.988B
raise 65.204B (±20.1%) i/s - 183.890B
Comparison:
if: 317735485266.5 i/s
raise: 65203928436.3 i/s - 4.87x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.