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";