Skip to content

Instantly share code, notes, and snippets.

@kenchan
Created September 6, 2018 07:30
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 kenchan/09e4dfabbbf9e68a5bcfe88fd2f4d5f6 to your computer and use it in GitHub Desktop.
Save kenchan/09e4dfabbbf9e68a5bcfe88fd2f4d5f6 to your computer and use it in GitHub Desktop.
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