Skip to content

Instantly share code, notes, and snippets.

@takai
Created January 18, 2011 00:23
Show Gist options
  • Save takai/783774 to your computer and use it in GitHub Desktop.
Save takai/783774 to your computer and use it in GitHub Desktop.
# -*- mode: ruby; coding: utf-8-unix -*-
require 'benchmark'
Benchmark.bm(10) do |bm|
a = nil
bm.report 'ternary op' do
10000.times { a ? a.name : 'unknown' }
end
bm.report 'rescue' do
10000.times { a.name rescue 'unknown' }
end
end
% ruby rescue.rb
user system total real
ternary op 0.000000 0.000000 0.000000 ( 0.002311)
rescue 0.100000 0.010000 0.110000 ( 0.118088)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment