Perl-Sensitive Sunglasses PerlMonks

### Cost of list assignment -

by mimiandi (Novice)
 on Jan 31, 2013 at 09:17 UTC Need Help??
mimiandi has asked for the wisdom of the Perl Monks concerning the following question:

Hi just wondering what the cost of doing list assignment as below in perl? Very useful, but wondering if this costs more than than doing it otherways

(\$num, \$num2) = (\$num3, \$num4);

Replies are listed 'Best First'.
Re: Cost of list assignment -
by BrowserUk (Pope) on Jan 31, 2013 at 09:38 UTC
but wondering if this costs more than than doing it otherways

Yes, measurably so:

```@a=1..1e6;
cmpthese -3,{
a => q[ @a[ \$_-1, \$_ ] = @a[ \$_, \$_-1 ] for 1 .. \$#a; ],
b => q[ my\$t; \$t = \$a[ \$_-1 ], \$a[ \$_-1 ] = \$a[ \$_ ], \$a[ \$_ ] = \$
+t for 1 .. \$#a; ]
};;
Rate   a   b
a 1.58/s  -- -5%
b 1.66/s  5%  --

but the difference is tiny, so only becomes significant in inner loops with high iteration factors.

Other times the clarity is worth the cost.

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Cost of list assignment -
by choroba (Bishop) on Jan 31, 2013 at 09:34 UTC
If you are getting paid for the number of lines, it costs you one line.

The real power of list assignment is

```(\$x, \$y) = (\$y, \$x);
No temporary variable!
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Create A New User
Node Status?
node history
Node Type: perlquestion [id://1016263]
Approved by Corion
help
Chatterbox?
 [holli]: the fricking beast will only eat dry food [holli]: nothing else, not even minced meat or fish [Discipulus]: see you nuns and monks! [KurtZ]: mice? [Discipulus]: your cat is dryarian holli [holli]: not even remotely KurtZ. i did find feathers in the garden a few times though [holli]: yet no body [virtualsue]: I was called upon to remove a gnawed mouse carcass from the living room this morning

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (11)
As of 2017-11-21 12:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
In order to be able to say "I know Perl", you must have:

Results (300 votes). Check out past polls.

Notices?