Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Perl script performance help

by G0G0 (Initiate)
on Jul 05, 2016 at 09:02 UTC ( [id://1167213]=perlquestion: print w/replies, xml ) Need Help??

G0G0 has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: Perl script performance help
by Ratazong (Monsignor) on Jul 05, 2016 at 09:32 UTC

    Hi G0G0

    Why do you need the @array? You could improve the required memory by changing

    my @array = (1..1000000000); foreach (@array) {
    to
    foreach (1..1000000000) {
    (The first solution gives an "out of memory" on my system, the later runs in 50 seconds with an empty loop).

    Inside the loop, you can probably save some time by reversing only once, and storing the intermediate result in a temporary variable. And based on the size of the current number, you could determine which of the substr-lines is relevant and skip the others...

    HTH, Rata

      Hi Rata thanks for the help, you are right your approach saved me a lot of memory, but i dont really understand what you mean in the last paragraph. thanks George

        He was suggesting you don't have to do the reverse 7 times. I don't think that's true.

        He also suggested you test the size of the variable and don't perform the substr() commands that will just return undef.

        my $start_run = time(); use strict; use Benchmark; use warnings; my @array = (1..1000000000); foreach (@array) { for(my $i=1; $i<length($_);$i++) { if ($_==reverse(substr($_, 0, $i) * substr($_, $i))) { print $_."\n" ;} } } my $end_run = time(); my $run_time = $end_run - $start_run; print "Time taken: ".$run_time." sec\n";
        But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

Re: Perl script performance help
by BrowserUk (Patriarch) on Jul 05, 2016 at 16:26 UTC

    I have a single observation for you: 34 * xxx can never produce xxx43.

    Because 4 * any digit will never produce a 3 as the last digit of the result.

    That single observation will allow you to cut your runtime by (several) orders of magnitude.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Perl script performance help
by BrowserUk (Patriarch) on Jul 05, 2016 at 10:13 UTC

    Is there a purpose to this?


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.

      The algorithm finds the members of some class of interesting or magic numbers. I suspect the purpose for performing the calculation was to exercise the CPU.

      But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

Re: Perl script performance help (A name or keyword?)
by BrowserUk (Patriarch) on Jul 07, 2016 at 15:30 UTC

    Do you -- or anyone -- know if there is a name or keyword that describes this problem?


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1167213]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-19 00:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found