Ruby on Rails has a pretty big impact I bet. Using eval on tainted input is a security disaster waiting to happing. Using the proc creation methods shown here would be just as bad from that point of view.
Another big reason is that debugging can easily become a nightmare.
@DoubleX:
Thank you for the performance data 😀
Have you tried testing the perfomance impact of having multiple different pieces of code.
What if say 1000 different pieces of codes each are run 1000 times? What if it's 10000 pieces of code run 100 times? What about other distributions?
*hugs*
- Zeriab
My guess is that the more resource consuming the script code is, the closer the performance between eval, proc/lambda and instance_eval. Also proc and lambda almost have the same time performance in all of my test cases.
For example:
Spoiler
block1 = " r = [] 1_000.times { r.push((rand * 1_000).to_i.to_s.to_sym.id2name.to_f / 1_000) } r.sort! { |a, b| a <=> b }"block2 = " r = rand 1_000.times { r = [r] } 1_000.times { r = r[0] }"blocks = [block1, block2]class Test def control1 r = [] 1_000.times { r.push((rand * 1_000).to_i.to_s.to_sym.id2name.to_f / 1_000) } r.sort! { |a, b| a <=> b } end def control2 r = rand 1_000.times { r = [r] } 1_000.times { r = r[0] } end def make_def(method) instance_eval(method) endendtest = Test.newblocks.each_with_index { |block, index| test.make_def(%Q( def new_block#{index + 1} #{block} end ))}#~ # block#~ p("block")#~ 10_000.times {#~ r = []#~ 1_000.times { r.push((rand * 1_000).to_i.to_s.to_sym.id2name.to_f / 1_000) }#~ r.sort! { |a, b| a <=> b }#~ r = rand#~ 1_000.times { r = [r] }#~ 1_000.times { r = r[0] }#~ }#~ p("block")#~ # Roughly 49 seconds#~ # method#~ p("method")#~ 10_000.times {#~ test.control1#~ test.control2#~ }#~ p("method")#~ # Roughly 49 seconds#~ # instance_eval#~ p("instance_eval")#~ 10_000.times {#~ test.new_block1#~ test.new_block2#~ }#~ p("instance_eval")#~ # Roughly 49 seconds#~ # lambda#~ test = eval(#~ "lambda {#~ #{block1}#~ #{block2}#~ }")#~ p("lambda")#~ 10_000.times { test.call }#~ p("lambda")#~ # Roughly 50 seconds#~ # proc#~ test = eval(#~ "proc {#~ #{block1}#~ #{block2}#~ }")#~ p("proc")#~ 10_000.times { test.call }#~ p("proc")#~ # Roughly 50 seconds#~ # eval#~ p("eval")#~ 10_000.times {#~ eval(block1)#~ eval(block2)#~ }#~ p("eval")#~ # Roughly 51 seconds
All the direct block, direct method call, instance_eval, lambda/proc and eval are run 10,000 times and the time needed are roughly 49, 49, 49, 50, 50 and 51 seconds respectively.
It suggests that their speeds are almost the same if the script code reaches this level of complexity.
Another example:
Spoiler
class Test def control r = [] 10000.times { r.push((rand * 10000).to_i.to_s.to_sym.id2name.to_f / 10000) } r.sort! { |a, b| a <=> b } end def make_def(method) instance_eval(method) endendblock = " r = [] 10000.times { r.push((rand * 10000).to_i.to_s.to_sym.id2name.to_f / 10000) } r.sort! { |a, b| a <=> b }"test = Test.newtest.make_def(%Q( def new_def #{block} end))#~ p("block")#~ 1_000.times { #~ r = []#~ 10000.times { r.push((rand * 10000).to_i.to_s.to_sym.id2name.to_f / 10000) }#~ r.sort! { |a, b| a <=> b }#~ }#~ p("block")#~ # Roughly 56 seconds#~ p("method")#~ 1_000.times { test.control }#~ p("method")#~ # Roughly 56 seconds#~ p("instance_eval")#~ 1_000.times { test.new_def }#~ p("instance_eval")#~ # Roughly 56 seconds#~ test = eval("lambda { #{block} }")#~ p("lambda")#~ 1_000.times { test.call }#~ p("lambda")#~ # Roughly 56 seconds#~ test = eval("proc { #{block} }")#~ p("proc")#~ 1_000.times { test.call }#~ p("proc")#~ # Roughly 56 seconds#~ p("eval")#~ 1_000.times { eval(block) }#~ p("eval")#~ # Roughly 56 seconds
It's of course an even more unrealistic example but it further suggests that if the script code is complicated enough, all direct block, direct method call, instance_eval, lambda/proc and eval almost have the same speed as they all run 1000 times in roughly 56 seconds.
It seems to me that the time needed mainly consists of 2 components:
1. Time needed to call the block
2. Time needed to execute the block
According to my informal benchmarks, the speed of direct block, direct method call, instance_eval, lambda/proc and eval almost solely differs from the time needed to call the block. I suspect that it's because they all execute the block in similar ways, but the ways they call the block are quite different from each other(for example, eval needs to parse the script code string into executable script code before executing it). If that's the case, then it can explain why the more complicated the script code is, the closer the speed of the above ways, as most of the time is spent on executing the block itself, rather than calling the block.
On the contrary, in the 1st benchamrk of my previous reply, the script code is "nil". Obviously the time needed to execute the block is trivial, so almost all the time is spent on calling the block instead. Eval performs the worst here, as it's more than 17 times slower than proc/lambda and more than 30 times slower than instance_eval in that benchmark. Even the much, much more efficient proc/lambda are still slower than instance_eval by 80%.
My conclusion about lambda/proc vs eval:
1. Don't use proc/lambda if the script code is rarely executed, use eval in this case instead, as proc/lambda probably consumes more memory than eval.
2. Don't use eval if the script code is frequently executed and time is more critical than memory, unless the script code reaches the complexity of that in the 1st benchmark in this reply.
P.S.: Actually I'm learning instance_eval and class_eval, and it seems to me that they are even better than proc/lambda in terms of performance. Creating more methods on the fly may cost more memory, but I still think the speed outweights that drawback. Plus, using instance_eval and class_eval need not worry about saving the methods created on they fly(at least according to my tests), while saving proc/lambda indirectly will be a bit tricky.