On Sat, Jun 15, 2013 at 12:10 AM, Faelin McCaley Landy < faelin.la...@gmail.com> wrote:
> Random question regarding optimization: for relatively small objects (five > or six attributes) that need updating, is it more efficient in Moose to > generate a new object, or to access an attribute and update it? > > NOTE: in order to avoid errors in my program, the existence of the object > must be checked in a (possibly large) hash before updating, whereas > generation can be done spontaneously. > > Hi Faelin, I'm a Moose newbie, so I'm answering with vague generalities that may or may not be helpful. The first not-specific-to-Moose thought that comes to mind is that you could use Benchmark to find your answer experimentally. http://perldoc.perl.org/Benchmark.html [note: my "sample code" below is fake, untested, might not compile] You would write a subroutine that does the "generate a new one" and one that does the "modify an existing one" and compare them with a "timethese( $count, "using new" => sub { ...}, "using existing" => sub {...} )" and see what the output looks like. Another not-specific-to-Moose thought is that hash lookups are really fast. So if your "NOTE:" part is just talking about something like if (my $existing_object = $my_big_hash_of_objects{ $object_id } ) { # manipulate existing object } then that is highly unlikely to be your bottleneck. But, Benchmark.pm is so easy to use that it's often faster to create a test of something than it is to fully formulate your question :). I would give that a shot first--it's a great thing to add to your toolkit. > I know, I know, this question is fairly vague. I'm happy to explain the > context of my problem, if that helps. I would be interested to hear more context if you don't mind explaining. mike