Skip to content

Instantly share code, notes, and snippets.

@kouyaf77
Last active January 9, 2019 06:58
Show Gist options
  • Save kouyaf77/48105b64a937c8fd9bd237b67c4d62ba to your computer and use it in GitHub Desktop.
Save kouyaf77/48105b64a937c8fd9bd237b67c4d62ba to your computer and use it in GitHub Desktop.
Ruby例外処理の計測
# Ruby 2.5.1
# ----- Result ------
# Normal: 0.0035529999986465555s
# Exception: 0.08799100000032922s
# Exception/Normal: 24.765268796467108
# Catch&Throw: 0.027447000000393018s
# Catch&Throw/Normal: 7.725021111975346
require 'benchmark'
exception_result = Benchmark.realtime do |b|
100000.times do
begin
raise
rescue
end
end
end
normal_result = Benchmark.realtime do |b|
100000.times do
end
end
throw_result = Benchmark.realtime do |b|
100000.times do
catch :test do
throw :test
end
end
end
p "Normal: #{normal_result}s"
p "Exception: #{exception_result}s"
p "Exception/Normal: #{exception_result/normal_result}"
p "Catch&Throw: #{throw_result}s"
p "Catch&Throw/Normal: #{throw_result/normal_result}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment