From: Phillip Gawlowski Date: 2010-11-25T21:44:03+09:00 Subject: Re: instance_eval vs attr_reader On Thu, Nov 25, 2010 at 1:07 PM, Joe Pikachu wrote: > Hi, I'm new in forum; even newer in programming. > I would like to know what is faster (uses less memory) to retrieve data? > I use ruby 1.8 series. speed and memory requirements are not related to each other. It's faster to store a large value in memory if it takes a million cycles to calculate it, and only 1000 to retrieve it, for example. > my_class.instance_eval(@var) or > my_class.var #using attr_reader > > My question is based on the fact one have to asign a symbol to use the > 'attr_reader' type thus creating 'memory leaks', while not in the > former. But I'm not sure. You only create a memory leak in Ruby, if your objects are never garbage collected. So, if you create an object with 10 000 symbols, and immediately discard it, the memory will be reclaimed. Though, the question is: Why worry about memory, especially a tiny chunk of it in case of symbols, compared to the Ruby interpreter? As Robert pointed out, you need to measure (with Benchmark and memory profiling), before you should optimize where an optimization may not be needed (or even effective!). -- Phillip Gawlowski Though the folk I have met, (Ah, how soon!) they forget When I've moved on to some other place, There may be one or two, When I've played and passed through, Who'll remember my song or my face.