-
-
Save Bartuz/2bf688bdd600588ed9b9 to your computer and use it in GitHub Desktop.
cost of using exceptions for control flow compared to one SQL statement (ruby 2.1.4, rails 4.1.7, sqlite) for rails-refactoring.com . Development mode executed in rails console.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark' | |
ActiveRecord::Base.logger = nil | |
Benchmark.bmbm do |bench| | |
bench.report("SQL query") do | |
1000.times { Whatever.count } | |
end | |
bench.report("exception hit") do | |
1000.times do | |
begin | |
raise StandardError.new | |
rescue | |
end | |
end | |
end | |
bench.report("exception miss") do | |
1000.times do | |
begin | |
raise StandardError.new if false | |
rescue | |
end | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rehearsal -------------------------------------------------- | |
SQL query 0.180000 0.010000 0.190000 ( 0.199253) | |
exception hit 0.050000 0.000000 0.050000 ( 0.050654) | |
exception miss 0.000000 0.000000 0.000000 ( 0.000050) | |
----------------------------------------- total: 0.240000sec | |
user system total real | |
SQL query 0.180000 0.010000 0.190000 ( 0.179669) | |
exception hit 0.050000 0.000000 0.050000 ( 0.048447) | |
exception miss 0.000000 0.000000 0.000000 ( 0.000054) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment