Created
October 28, 2011 23:57
-
-
Save mkdynamic/1323871 to your computer and use it in GitHub Desktop.
Rack Approach Benchmark: Class vs. Block vs. Method
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' | |
class App | |
def call(env) | |
[200, {'Content-Type' => 'text/html'}, ['ohai']] | |
end | |
end | |
@object = App.new | |
@lambda = lambda do |env| | |
[200, {'Content-Type' => 'text/html'}, ['ohai']] | |
end | |
@proc = Proc.new do |env| | |
[200, {'Content-Type' => 'text/html'}, ['ohai']] | |
end | |
def app(env) | |
[200, {'Content-Type' => 'text/html'}, ['ohai']] | |
end | |
@method = method(:app) | |
n = 1_000_000 | |
Benchmark.bmbm do |bm| | |
bm.report('object') do | |
n.times { @object.call({}) } | |
end | |
bm.report('lambda') do | |
n.times { @lambda.call({}) } | |
end | |
bm.report('proc') do | |
n.times { @proc.call({}) } | |
end | |
bm.report('method') do | |
n.times { @method.call({}) } | |
end | |
end |
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
[mbpro:~/documents/mkdynamic/labs] | |
→ ruby -v | |
ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin10.7.0] | |
[mbpro:~/documents/mkdynamic/labs] | |
→ ruby speed.rb | |
Rehearsal ------------------------------------------ | |
object 1.750000 0.010000 1.760000 ( 1.758695) | |
lambda 1.910000 0.000000 1.910000 ( 1.915731) | |
proc 1.930000 0.000000 1.930000 ( 1.937481) | |
method 1.900000 0.010000 1.910000 ( 1.905207) | |
--------------------------------- total: 7.510000sec | |
user system total real | |
object 1.770000 0.000000 1.770000 ( 1.772592) | |
lambda 1.890000 0.000000 1.890000 ( 1.895088) | |
proc 1.930000 0.010000 1.940000 ( 1.935838) | |
method 1.920000 0.000000 1.920000 ( 1.923905) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment