Skip to content

Instantly share code, notes, and snippets.

@seban
Created August 2, 2017 09:38
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 seban/3fbddcdd75c3876e68c9397b8ada7fd0 to your computer and use it in GitHub Desktop.
Save seban/3fbddcdd75c3876e68c9397b8ada7fd0 to your computer and use it in GitHub Desktop.
require 'benchmark'
def test_exception
raise "test exception" unless true
2 + 2
rescue
4
end
def test_unless
unless true
2 + 2
else
4
end
end
def test_if
if true
4
else
2 + 2
end
end
Benchmark.bm do |x|
x.report("exception") { 10_000_000.times { test_exception } }
x.report("unless") { 10_000_000.times { test_unless } }
x.report("if") { 10_000_000.times { test_if }}
end
@katarzynabanc
Copy link

Thx for this compare :)
maybe a small visual improvements like:

  x.report("exception\t") ...
  x.report("unless\t\t") ...
  x.report("if\t\t") ...

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