I don't know. It returns the following really fast for me
Global symbol "@list1" requires explicit package name at 733911.pl lin +e 5. Global symbol "$count1" requires explicit package name at 733911.pl li +ne 9. Global symbol "@list1" requires explicit package name at 733911.pl lin +e 9. Global symbol "$num1" requires explicit package name at 733911.pl line + 10. Global symbol "$num2" requires explicit package name at 733911.pl line + 12. Global symbol "$indx" requires explicit package name at 733911.pl line + 14. Global symbol "$count1" requires explicit package name at 733911.pl li +ne 15. Global symbol "$var1" requires explicit package name at 733911.pl line + 17. Global symbol "$num1" requires explicit package name at 733911.pl line + 17. Global symbol "@list1" requires explicit package name at 733911.pl lin +e 17. Global symbol "$indx" requires explicit package name at 733911.pl line + 17. Global symbol "$indx" requires explicit package name at 733911.pl line + 18. Global symbol "$var2" requires explicit package name at 733911.pl line + 19. Global symbol "$num2" requires explicit package name at 733911.pl line + 19. Global symbol "@list1" requires explicit package name at 733911.pl lin +e 19. Global symbol "$indx" requires explicit package name at 733911.pl line + 19. Global symbol "$indx" requires explicit package name at 733911.pl line + 20. Global symbol "$var3" requires explicit package name at 733911.pl line + 21. Global symbol "@list1" requires explicit package name at 733911.pl lin +e 21. Global symbol "$indx" requires explicit package name at 733911.pl line + 21. Global symbol "$indx" requires explicit package name at 733911.pl line + 22. Execution of 733911.pl aborted due to compilation errors.

It'll also complete much sooner if it wasn't an infinite loop. What's with the while ()?

Moving on, I'm not sure we can optimize your code if you don't show it. It's "# further code do anything" that gets executed 181,503 times. It's "# further code do anything" that's going to affect the performance.

You might get a tiny gain by rearranging your loops

for ( [ test1 => 10, 22 ], [ test2 => 31, 17 ], [ test3 => 4, 45 ], ) { my ($test, $base1, $base2) = @_; for my $num1 (200..400) { my $var1 = $base1 + $num1; for $num2 (300..600) { my $var2 = $base2 + $num2; ... } } }

Any maybe another tiny gain by eliminating some arithmetic

for ( [ test1 => 10, 22 ], [ test2 => 31, 17 ], [ test3 => 4, 45 ], ) { my ($test, $base1, $base2) = @_; my $var1 = $base1 + 200; for (200..400) { my $var2 = $base2 + 300; for (300..600) { ... ++$var2; } ++$var1; } }

Update: Fixed bug in second snippet.


In reply to Re: faster way for multiple foreach loops by ikegami
in thread faster way for multiple foreach loops by michbach

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.