Created
December 7, 2012 23:51
-
-
Save sdeming/4237611 to your computer and use it in GitHub Desktop.
Proc wrapped eval, eval only once...
This file contains hidden or 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 ProcWrappedEval | |
attr_accessor :callable | |
def initialize(code) | |
@callable = eval("::Proc.new { #{code} }") | |
end | |
end | |
iterations = 1000000 | |
test_code = "6 * 7" | |
callable = ProcWrappedEval.new(test_code).callable | |
Benchmark.bmbm do |x| | |
x.report("proc wrapped eval:") do | |
iterations.times { instance_eval(&callable) } | |
end | |
x.report("instance_eval:") do | |
iterations.times { instance_eval test_code } | |
end | |
end |
This file contains hidden or 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
Rehearsal ------------------------------------------------------ | |
proc wrapped eval: 1.020000 0.030000 1.050000 ( 0.605000) | |
instance_eval: 2.950000 0.030000 2.980000 ( 2.273000) | |
--------------------------------------------- total: 4.030000sec | |
user system total real | |
proc wrapped eval: 0.200000 0.000000 0.200000 ( 0.204000) | |
instance_eval: 1.800000 0.010000 1.810000 ( 1.767000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Results: