Created
July 5, 2009 22:56
-
-
Save raggi/141151 to your computer and use it in GitHub Desktop.
This file contains 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" | |
@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