i am new to perl and want some help in tuning this script if possible cause it takes time if i increased my array. also if i "use warnings" i get a lot of warnings which im unable to handle. im using perl (v5.24.0) built for darwin-thread-multi-2level

my $start_run = time(); use strict; use Benchmark; use warnings; my @array = (1..1000000000); foreach (@array) { if ($_==reverse(substr($_, 0, 1) * substr($_, 1))) { print $_."\n" ;} if ($_==reverse(substr($_, 0, 2) * substr($_, 2))) { print $_."\n" ;} if ($_==reverse(substr($_, 0, 3) * substr($_, 3))) { print $_."\n" ;} if ($_==reverse(substr($_, 0, 4) * substr($_, 4))) { print $_."\n" ;} if ($_==reverse(substr($_, 0, 5) * substr($_, 5))) { print $_."\n" ;} if ($_==reverse(substr($_, 0, 6) * substr($_, 6))) { print $_."\n" ;} if ($_==reverse(substr($_, 0, 7) * substr($_, 7))) { print $_."\n" ;} } my $end_run = time(); my $run_time = $end_run - $start_run; print "Time taken: ".$run_time." sec\n";

thanks to everyone who has helped, i have now been able with the new ideas to write the code this way which has a good performance now </>

use strict; use warnings; use Time::HiRes qw( time ); my $start_run = time(); for my $n (100..1_00_000_000) { my $r = reverse($n); my $x = 100; while ($x <= $n) { if (int($n / $x) * ($n % $x) == $r) { print "$n\n"; last; } $x *= 10; } } my $end_run = time(); my $run_time = $end_run - $start_run; print "Time taken: ".$run_time." sec\n";

In reply to Perl script performance help by G0G0

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.