From: Sean O'Halpin Date: 2005-10-22T21:36:07+09:00 Subject: Re: Reflection, observ(er|able) instance vars? On 10/21/05, Hugh Sasse wrote: > This "delivers us from eval" :-) but does define_method offer > anything else over the class_eval %{...} approach? Maybe [only :-)] > speed? Yes, but not perhaps what you were expecting (2 files below): # file 1: compare def vs block method call overhead require 'benchmark' include Benchmark eval %[ def def_method(x) x end ] Object.send(:define_method, :defined_method) { |y| y } block_call = proc {|z| z } BM = 100000 bm(20) {|x| x.report("def") { BM.times { def_method(1) }} x.report("define_method") { BM.times { defined_method(1) }} x.report("block_call") { BM.times { block_call[1] }} } __END__ user system total real def 0.070000 0.000000 0.070000 ( 0.070000) define_method 0.240000 0.000000 0.240000 ( 0.251000) block_call 0.250000 0.000000 0.250000 ( 0.250000) # file 2: compare cost of eval vs define method require 'benchmark' include Benchmark BM = 100000 bm(20) {|x| x.report("eval def") { BM.times { eval "def def_method(x) x end" }} x.report("define_method") { BM.times { Object.send(:define_method, :defined_method) { |y| y } }} x.report("block_call") { BM.times { block_call = proc {|z| z } }} } __END__ user system total real eval def 2.744000 0.000000 2.744000 ( 2.744000) define_method 3.585000 0.040000 3.625000 ( 3.686000) block_call 1.191000 0.050000 1.241000 ( 1.252000) Regards, Sean (P.S. This is on WinXP SP2 Dell D600 laptop with 1.6GHz Pentium + 1GB RAM)