In general, when you have questions like this the real answer is to try it. The Benchmark module is your friend:
gives me:use Benchmark qw/ cmpthese /; cmpthese(-5, { outside => q{ my $scalar; for (1..10_000) { $scalar = 1; } }, inside => q{ for (1..10_000) { my $scalar = 1; } }, });
Rate inside outside inside 261/s -- -15% outside 307/s 18% --
I'm not sure why the 'outside' version is faster, and it intrigues me - but if all you care about is which is faster, you don't need to know.
Be careful about reading the percentages without noticing the rate though - since the assignment is happening 10,000 times for each iteration, we're talking about a 15% difference in speed for an operation that takes about a third of a microsecond. That is, you need to perform such an assignment about 18 million times before you can save 1 second by declaring the variable outside the loop. In most cases the clarity achieved by declaring the variable at the innermost scope far outweighs such microscopic savings.
Hugo
In reply to Re: Re: Re: Two Questions on "my"
by hv
in thread Two Questions on "my"
by C_T
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |