Roy Johnson has asked for the wisdom of the Perl Monks concerning the following question:
It has become fairly popular to chain calls to map, kind of like piping commands together. When I see those constructs, I wonder whether Perl is doing anything clever to avoid unnecessary copying, or if each call to map really does construct a new list, which the next map goes through to construct yet another list. How much memory overhead are we talking about?
After some pondering, it occurred to me that a construct like
is functionally equivalent tomap { BLOCK2; (return_list2) } map { BLOCK1; (return_list1) } @input;
but the BLOCK2 map is called for each return_list1, rather than once on a list that is @input times as big. The actual BLOCK2 code gets executed the same number of times. So the questions are:map { BLOCK1; local $_ = $_; map { BLOCK2; (return_list2) } (return_li +st1) } @input
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: map: chaining vs. nesting
by diotalevi (Canon) on Jun 18, 2004 at 14:10 UTC | |
by Roy Johnson (Monsignor) on Jun 18, 2004 at 15:55 UTC | |
by diotalevi (Canon) on Jun 18, 2004 at 16:23 UTC | |
|
Re: map: chaining vs. nesting
by hardburn (Abbot) on Jun 18, 2004 at 14:30 UTC | |
by diotalevi (Canon) on Jun 18, 2004 at 14:35 UTC | |
by danderson (Beadle) on Jun 18, 2004 at 18:42 UTC | |
by Errto (Vicar) on Jun 19, 2004 at 01:17 UTC | |
by FoxtrotUniform (Prior) on Jun 18, 2004 at 18:54 UTC | |
|
Re: map: chaining vs. nesting
by ihb (Deacon) on Jun 19, 2004 at 20:37 UTC |