[Nh-pm] The Perly Way
Paul L Lussier
pll at lanminds.com
Wed May 14 14:16:39 CDT 2003
In a message dated: Wed, 14 May 2003 14:48:55 EDT
Ray Cote said:
>> ... so I would have gone after the efficiency of the the third
>To expand my Perl knowledge a little bit more, do you feel the third
>example is more efficient because it does not have the overhead of
>building a second array?
In short yes. The third is nothing more than a refinement of the
second. Technically, the second probably uses slightly less memory,
since it just spits out each element as it appears. However, not
wanting a stray \t at the end of every line (which could introduce
line-wrapping issues) I'd rather incur the overhead of storing
everything in $string, then printing each complete row out all at
once, rather than in pieces.
>However, what is the efficiency of always appending to the $string on
>each pass vs doing a join (though, for all I know, join may simply
>iterate and append as well).
I have no idea what the difference between .= and a join are as far
as efficiency. The .= though, allows you to operate on each element
before printing it, whereas a join does not. Really, I think it's a
wash in this case. Any of the 3 methods is slightly more efficient
than the pre-processing you started out with. I don't by how much,
since it completely depends upon your dataset. But having to process
each row twice, basically adds an exponential amount of time your
processing, right?
>> (which incidently, has a
>>minor bug in it, can you spot it :)
>You didn't declare my $string?
Well, declaration is irrellevent in perl in most cases. More
specifically, I do not clear the contents of $string after each pass
of the loop. So, since I use the .= operator, I end up appending
each row to the next for the duration of the loop.
This problem can be solved in 2 ways. Either, create $string as a
my()'ed variable local to the foreach, so that it remains in scope
for the map, but gets re-defined for each new row, or just clear the
variable at the beginning of each pass of the foreach loop. In
either case, the statement would just before the map() call.
Clearing the variable probably is the most efficient, since you only
allocate that memory once, where as my()'ing the variable probably
allocates a new chunk of memory through each pass. The difference in
performance is probably negligible, but, it's good to be aware of the
implications of different methods :)
--
Seeya,
Paul
--
Key fingerprint = 1660 FECC 5D21 D286 F853 E808 BB07 9239 53F1 28EE
It may look like I'm just sitting here doing nothing,
but I'm really actively waiting for all my problems to go away.
If you're not having fun, you're not doing it right!
More information about the Nh-pm
mailing list