Skip to content

Instantly share code, notes, and snippets.

@sdeming
Created December 7, 2012 23:51
Show Gist options
  • Save sdeming/4237611 to your computer and use it in GitHub Desktop.
Save sdeming/4237611 to your computer and use it in GitHub Desktop.
Proc wrapped eval, eval only once...
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
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)
@sdeming
Copy link
Author

sdeming commented Dec 7, 2012

Results:

Rehearsal ------------------------------------------------------
proc wrapped eval:   1.080000   0.020000   1.100000 (  0.571000)
eval:                3.370000   0.030000   3.400000 (  2.363000)
--------------------------------------------- total: 4.500000sec

                         user     system      total        real
proc wrapped eval:   0.200000   0.000000   0.200000 (  0.199000)
eval:                1.800000   0.000000   1.800000 (  1.756000)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment