Skip to content

Instantly share code, notes, and snippets.

@rsofaer
Created March 27, 2011 19:06
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 rsofaer/889478 to your computer and use it in GitHub Desktop.
Save rsofaer/889478 to your computer and use it in GitHub Desktop.
Got curious after seeing something on reddit/r/ruby
require 'benchmark'
module RoflModule
def rofladd(ping, pong)
ping + pong
end
end
RoflBlock = <<BLOCK
def rofladd(ping, pong)
ping + pong
end
BLOCK
class Modulefer
include RoflModule
end
class Blockefer
class_eval RoflBlock
end
puts "With Module"
puts Benchmark.measure { 1000000.times { Modulefer.new.rofladd(60,9) } }
puts "With Block"
puts Benchmark.measure { 1000000.times { Blockefer.new.rofladd(60,9) } }
Result:
rvm is a function
Using /Users/raphael/.rvm/gems/ruby-1.8.7-p334
With Module
0.970000 0.000000 0.970000 ( 0.975904)
With Block
0.970000 0.000000 0.970000 ( 0.971186)
Using /Users/raphael/.rvm/gems/ree-1.8.7-2011.03
With Module
0.880000 0.000000 0.880000 ( 0.891846)
With Block
0.890000 0.000000 0.890000 ( 0.893295)
Using /Users/raphael/.rvm/gems/ruby-1.9.2-p180
With Module
0.410000 0.000000 0.410000 ( 0.417497)
With Block
0.410000 0.000000 0.410000 ( 0.412741)
Using /Users/raphael/.rvm/gems/rbx-head
With Module
0.224987 0.003841 0.228828 ( 0.275128)
With Block
0.115802 0.000301 0.116103 ( 0.119960)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment