Perl: Why is it slower to declare (my) variables inside a loop?

What is the difference, from the POV interpreter, between the following programs:

#!/usr/bin/perl -w use strict; for (1..10000000) { my $jimmy = $_**2; } 

and

 #!/usr/bin/perl -w use strict; my $jimmy; for (1..10000000) { $jimmy = $_**2; } 

"time" for the first program:

 real 0m1.519s user 0m1.513s sys 0m0.004s 

and for the second:

 real 0m1.023s user 0m1.012s sys 0m0.002s 
+6
perl interpreter strict
source share
5 answers

The declaration of my in Perl has two main effects; compilation time (in which he selects a slot on the containing notepad and ensures that all references to this name within the correct area are resolved to this particular notepad slot) and one time interval (in which he resets the value of this slot for laying to undef or to a specific value if you wrote my $var = foo ).

Of course, part of the compilation time has zero amortized cost of execution, but part of the runtime is triggered every time the execution passes my declaration. As others pointed out, your two examples have different performance, because they have different semantics in general - each time it clears a variable every cycle, and the other does not.

+10
source share

Since the examples of the programs that you indicated do not actually do anything, itโ€™s difficult for you to give a specific reason why one type of declaration will be better than another. As noted by many other posters, declaring a variable in a loop creates a new variable each time. In your examples, that creation is redundant, but consider the following examples using closure.

 my @closures; my $jimmy; for (1 .. 10) { $jimmy = $_** 2; push @closures, sub {print "$jimmy\n"}; } 

and this one:

 my @closures; for (1 .. 10) { my $jimmy = $_** 2; push @closures, sub {print "$jimmy\n"}; } 

In each case, the code creates a series of links to the code, but in the first example, since all links to the code refer to the same $jimmy , each of them will print 100 when called. In the second example, each ref code will print a different number (1, 4, 9, 16, 25, ...)

So, in this case, the time difference does not matter much, since the two blocks of code have different things.

+3
source share

The first loop tries to make a variable declaration for each iteration of the loop and may lead to unnecessary processing time.

Of course, this is not so much, but this material can develop over time, and it is technically slower, since more instructions are executed for each iteration.

+2
source share

Well, there is a problem that you are declaring a new variable with each iteration.

Secondly, there is a big review problem.

Try adding this line after for in each of them and see what happens:

 print $jimmy; 

And, try this as well:

 my $jimmy; for (1..10000000) { my $jimmy = $_**2; } print $jimmy; 

A bit more detailed:

A mine declares the listed variables to be local (lexical) to the attached block, file, or eval. If more than one value is specified, the list must be placed in parentheses.

http://perldoc.perl.org/functions/my.html

Most likely, it will be useful to read:

http://perldoc.perl.org/perlsub.html#Private-Variables-via-my%28%29

+1
source share
  • Declaring my out of loop causes the declaration to occur once. At the time of declaration, perl reserves memory for this variable.

  • The declaration of my inside the loop causes the declaration to appear on each interval of the loop.

my is Perlโ€™s response to declaring a variable locally - local used for something else and doesnโ€™t mean the same as in C. When you declare a variable inside a loop, it is declared in the local scope in a loop block, where the block starts / ends at each interval. Not only the declared variable, but can also be cleared (dereferenced and / or set to undef ) at the end of the block (although this varies from Perl versions).

Variables declared outside the loop are considered "global" (not literally, but in the sense of a loop). These variables reuse their memory locations instead of looking for new addresses.

0
source share

All Articles