in reply to Cost of list assignment -
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.
|
|---|