Is $_ more efficient than a named variable in Perl's foreach? -


I am quite new in Perl and I want to know which of the following are more efficient:

my @ points = (1,3,5,7,9); My $ Current Foreach (@points) {Print "$ Current \ n"; }

or

  my @ digit = (1,3,5,7,9); Foreign (@points) {print "$ _ \ n"; }  

I want to know that the use of $ _ is more efficient because a register has a place because it is usually used or not. I have written some code and I am trying to clean it and I have come to know that I have been using loop more often than the second time.

Even old customization is the root cause of all evils

  {local $ \ = "\ n"; Print for @number; }  

But some expectations may be wrong. The test is a bit weird because the output can do some weird side effects and the order can be important.

  #! Strict / usr / bin / env perl; Use warnings; Use Benchmark QW (: all: hireswallclock); Use constant number = & gt; 10000; My @ digit = (number 1.); Sub-Noouts (& amp;) {Local * STDOUT; Open STDOUT, '& gt;', '/ dev / null'; My $ result = shift () - & gt; (); Closed STDOUT; Return result; }; My% test = (loop 1 = & gt; sub) my $ current (@points) {print "$ current \ n"}}}, loop 2 = & gt; Sub {foreach (numeral) {print "$ _ \ n";}}, loop 3 = & gt; Sub (local $ \ = "\ n"; printed lead @not;}); Sub-permutation {return [map {my $ a = $ _; My @f = grep {$ a $ _} @_; Map {[$ a, @ $ _]} @ {permutation (@f)}} @_] if @_; Return [[]]; } Foreach my $ p (@ {permutations (keys% test)}} {my $ result = {map {$ _ = & gt; No_out {sleep1; count (2, $ test {$ _})}} @ $ P}; cmpthese ($ result);}  

One can expect that Loop 2 should be faster than Loop 1

  Rate loop2 loop1 Loop3 loop2 322 / s - -2% -34% Loop 1 328 / s2% - -33% Loop 3 486 / S 51% 48% - Rate Loop 2 Loop 1 Loop 3 Loop 2 322 / S - -0% - 34% Loop 1 323 / S 0% -34% Loop 3 486 / S 51% 50% - Rate Loop 2 Loop 1 Loop 3 Loop 2 323 / S - -0% -33% Loop 1 324 / S% - - 33% Loop 3 484 / S 50% 49% - Rate Loop 2 Loop 1 Loop 3 Loop 2 317 / S -3% -35% Loop 1 328 / S3% - -33% Loop 3 488 / S 54% 49% - Rate Loop 2 Loop 1 Loop 3 Loop 2 323 / S - 2% -34% Loop 1 329 / S2% - -33% Loop 3 48 9 / S 51% 49% - Rate Loop 2 Loop 1 Loop 3 Loop 2 325 / S -1% -33% Loop 1 329 / S1% -32% Loop 3 488 / S 50% 48% -  

Sometimes I Looked 15% -20% faster than loop1 to loop2 but I did not know why.

I saw the generated byte code, while making the loop1 and loop2 and my variables only one difference is that this variable is not allocated to the internal and not even copied So this operation is very cheap. The difference comes I think that the only "$ _ \ n" is not cheaper than the creation. These loops must be very similar to

  (not @no points) {...} for my $ a {@ point} {...}  

But this loop is more expensive

 for  (no.) {My $ a = $ _; ...}  

more

  print "$ a \ n";  

  print $ a, "\ n";  

Comments