From: Robert Klemme Date: 2010-11-26T20:43:44+09:00 Subject: Re: instance_eval vs attr_reader On Fri, Nov 26, 2010 at 12:25 PM, Joe Pikachu wrote: > Thanks for everyone who replied. I MADE A MISTAKE IN MY ORIGINAL > EXAMPLE. > What I meant to compare speed/memory wise was: > > my_class.instance_eval {@var} vs. #NOT my_class.instance_eval(@var) > my_class.var #using attr_reader > > So the 'argument' is a block. Does it make any difference at all. What I > understand is that by using instance_eval self becomes my_class and one > can access the variable. This seems to my much easier that having to add > a > attr_reader-line-of-code every time one needs to access a variable from > outside. But is it practical, in the long run? Most Ruby programs use that approach. > @Stefano Crocco: With the new example @var none-string works. > @Robert Klemme: I can't use Benchmark at the moment. Why? It's not difficult: 12:40:07 ~$ ruby19 -r benchmark < T = 10000 > Benchmark.bm 10 do |x| > x.report("foo") { T.times { 1 + 2 } } > x.report("bar") { T.times { 1 * 2 } } > end > CODE user system total real foo 0.000000 0.000000 0.000000 ( 0.002000) bar 0.000000 0.000000 0.000000 ( 0.002000) 12:41:04 ~$ Replace "1+2" and "1*2" with the code you want to compare. Of course, you can put this in a script. Again, stop wondering, start measuring. > @Phillip Gawlowski: If I'm not mistaken, symblos are NEVER G.collected. > That's my concern. Since I'm making dozen of symbols. Also, can you give > me a good link on how memory/speed works in computer programming. I use > google, but I don't think I'm using the right keywords. "Dozens" is not actually what I would call "many". Don't worry. Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/