Skip to content

Instantly share code, notes, and snippets.

@raggi
Created July 5, 2009 22:56
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 raggi/141151 to your computer and use it in GitHub Desktop.
Save raggi/141151 to your computer and use it in GitHub Desktop.
require "benchmark"
@callbacks = %w(
TimerFired
ConnectionData
ConnectionUnbound
ConnectionAccepted
ConnectionCompleted
LoopbreakSignalled
ConnectionNotifyReadable
ConnectionNotifyWritable
SslHandshakeCompleted
)
@callbacks.each_with_index { |cb, i| Object.const_set(cb, i + 100) }
def make_class(name)
Class.new { eval "def call; #{name.inspect}; end" }
end
@callbacks.map! { |c| make_class(c.to_sym).new }
def elif(n)
if n == TimerFired
:TimerFired
elsif n == ConnectionData
:ConnectionData
elsif n == ConnectionUnbound
:ConnectionUnbound
elsif n == ConnectionAccepted
:ConnectionAccepted
elsif n == ConnectionCompleted
:ConnectionCompleted
elsif n == LoopbreakSignalled
:LoopbreakSignalled
elsif n == ConnectionNotifyReadable
:ConnectionNotifyReadable
elsif n == ConnectionNotifyWritable
:ConnectionNotifyWritable
elsif n == SslHandshakeCompleted
:SslHandshakeCompleted
end
end
def idex(n)
n = n - 100
@callbacks[n].call
end
TESTS = 100_000
Benchmark.bmbm do |results|
results.report("elif:") { TESTS.times { |n| elif( (n % 8) + 100 ) } }
results.report("idex:") { TESTS.times { |n| idex( (n % 8) + 100 ) } }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment