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

Hello all,

I have just started to teach myself perl. I am not completely new to programming, but I would say that I could barely qualify as an intermediate programmer. I've screwed around here and there since I was a kid with Q-Basic, a little origanl visual basic, C++ and python. Though I've never written a program of my own thats, I'd say, prob not more than 100 - 200 lines.

So, I've written this for my first perl learning project

#!/usr/bin/perl use strict; use warnings; sub fib { my $n = shift; return $n if $n < 2; return fib($n - 1) + fib($n - 2); } my $value = 0; my $range = 0; my $phiApprox = 0; my $i = 0; my $temp = 0; my $fibValue1 = 0; my $fibValue2 = 0; print "Fibonnaci Fun\n\n"; print "Pick a number that represents which value of the Fibonnaci sequ +ence you want to start with\n"; print "ie: 1 would be 1 and 2 would 1 and 3 would be 2 and so on.\n"; print "Your choice: "; chomp($value = <>); print "\nNow pick a number to repesent the range you want.\n"; print "If you want to use values 1 through 10 you you need only enter +10,\n"; print "assuming of course you entered 1 above.\n"; print "Your choice: "; chomp($temp = <>); $range = $value + $temp; for($i = $value; $i <= $range; $i++){ $fibValue1 = fib($i); $fibValue2 = fib($i - 1); if($i <= 1){$phiApprox = 0}; if($i >= 2){$phiApprox = $fibValue1 / $fibValue2}; print "\n"; print $fibValue1.' / '.$fibValue2.' = '.$phiApprox; } print "\n";

I was just wondering, for learning purposes, if there was something that could been done to speed up the program. Not that i expect to it run real fast at all, I know I'm dealing with large numbers. I am just wondering if there is any function or something that could speed up how perl handles larger numbers.

I would also appreciate any constructive criticism on my code, I am here to learn.

As I am knew here I hope I've done nothing wrong in formating and placing this post, and apologies in advance if I have

Replies are listed 'Best First'.
Re: how to speed up program dealing with large numbers?
by BrowserUk (Patriarch) on Mar 21, 2010 at 23:13 UTC

    Change your fib sub to the following. You'll be amazed at the speedup you get:

    my %cache; sub fib { my $n = shift; return $n if $n < 2; return ( $cache{ $n - 1 } ||= fib($n - 1) ) + ( $cache{ $n - 2 } ||= fib($n - 2) ); }

    With your fib(), 1 .. 50 takes 10 100 minutes and counting. With the above, 1 .. 100 takes less than 1 millisecond.

    Once you're done being amazed, see Memoize for the explanation of what it does and why it works.


    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".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      An array would be better suited than a hash since we're dealing with non-negative integer indexes.

      my @fib = ( 0, 1 ); sub fib { my $n = shift; return ( $fib[ $n - 1 ] ||= fib($n - 1) ) + ( $fib[ $n - 2 ] ||= fib($n - 2) ); }

      Sub calls are expensive in Perl, and there's no need for recursion here, so let's eliminate them:

      my @fib = ( 0, 1 ); sub fib { my $n = shift; $fib[$_] = $fib[$_-2] + $fib[$_-1] for @fib..$n; return $fib[$n]; }
        An array would be better suited than a hash since we're dealing with non-negative integer indexes.

        Even in this specific case, the difference in either performance or memory is so marginal as to be essentially unmeasurable.

        For 1..1000, both take around 1.3 seconds and negligable amounts of memory. Where the original implementation would take months if not years longer than the universe has existed!

        But using a hash for caching makes far more sense because of its generality.

        Giving up that generality to move from being 15,000,000 1e+196 times faster to being 15,000,001 1e+196 + 1 times faster is simply pointless. To put that into perspective, it means your changes on top of mine will make a difference of approximately:

        0.00000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000999999999999 +999999999999999999999999999999999999999999999999999999999999999999999 +999999999999999999999999999999999999999999999999999999999999999999999 +999999999999999999999999999999999999999999999900000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000009999999999999999999999999999999999 +999999999999999999999999999999999999999999999999999999999999999999999 +999999999999999999999999999999999999999999999999999999999999999999999 +999999999999999999999999000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000 +0000000000001%

        Worth the effort?

        Sub calls are expensive in Perl, and there's no need for recursion here, so let's eliminate them:

        Again, an utterly pointless exercise.

        When performing the range 1 .. 1000, with the cached version the fib function is called 3005 times. That's twice for each iteration from the main loop, leaving 1 recursion per iteration. Compare that to the original implementation that would call fib() ~1e200 times. That's:

        100,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,00 +0,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000 +,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000, +000,000,000,000,000,000,000,000,000,000,000,000,000,000,000

        So any additional savings are so utterly marginal as to be completely unmeasurable.


        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".
        In the absence of evidence, opinion is indistinguishable from prejudice.

      Awsome, oh yeah, I am amazed!! Huge difference!! Huge, huge, huge difference

      I still need to read about Memoize more thoroughly to fully understand it I think. It looks as though it could certainly be a useful tool. As it pertains to my program though, the concept is geniously simple, put the values in a hash instead of recalculating them a bunch of times, freakin great.

Re: how to speed up program dealing with large numbers?
by toolic (Bishop) on Mar 21, 2010 at 23:17 UTC
    The formatting of your post is fine, but you should choose a better node title. See How do I compose an effective node title?. This will help you to get a better response.

    With regards to speeding up your code, you could try to profile it. See the Tutorials section: Profiling your code.

    I would also appreciate any constructive criticism on my code, I am here to learn.
    perlcritic is a handy tool for automatically criticizing your code.
    for($i = $value; $i <= $range; $i++){
    A more Perl-ish way to write this for loop is:
    for $i ($value .. $range){
    Also
    print $fibValue1.' / '.$fibValue2.' = '.$phiApprox;
    can be written as (with less typing):
    print "$fibValue1/$fibValue2 = $phiApprox";

    Update: fixed link.

      Thank you for the information and criticism.

      I'll keep that in mind about title-ing in the future.

      As far a speeding up the code, BrowserUk showed me some code using Memoize that made a huge difference. Still, thanks for the tip on profiling, looks like that is something that could come in handy, I'm definatley gonna read up on that.

      Definitely want to check out that percritic, but your link seems broken. No big though, I'll google it.

      I love the format you suggested for the for loop, already changd that in my code. Much easier way of doing a for loop.

      Again, thank you, this is exactly the kind of info i am looking for.

Re: how to speed up program dealing with large numbers?
by GrandFather (Saint) on Mar 22, 2010 at 00:33 UTC

    If you want one line if statements use Perl's statement modifier form of if:

    $phiApprox = 0 if $i <= 1;

    for and while can be used that way too.


    True laziness is hard work

      Thanks for the tip. I likey, a little less typing. I am likeing perl more and more.

Re: how to speed up program dealing with large numbers?
by 7stud (Deacon) on Mar 22, 2010 at 03:45 UTC

    1) What version of perl are you using?

    2) As far as I can tell, this isn't really done much in perl:

    my $value = 0; my $range = 0; my $phiApprox = 0; my $i = 0; my $temp = 0; my $fibValue1 = 0; my $fibValue2 = 0;

    Declare your variables at (or near) first use, for example:

    chomp(my $value = <>); chomp(my $temp = <>); my $range = $value + $temp; my $total; for (0 .. $range) { my $fibValue1 = fib($i); my $fibValue2 = fib($i - 1); ... ... $total++; }

    Redeclaring variables inside loops is done with alacrity in perl.

    3) Note that you can do this in perl:

    use strict; use warnings; use 5.010; my $total; $total++; say $total; --output:-- 1

    ...which would not work in those other languages you know. Yet, paradoxically you will get a warning if you do this:

    use strict; use warnings; use 5.010; my $total; my $result = $total + 1; say $result; --output:-- Use of uninitialized value $total in addition (+) at 1perl.pl line 9. 1

      1) I have v5.8.9 installed, should i be putting that in a use line atop my code

      2) Good to know, yeah thats a C++ habbit. This makes me like perl even more.

      3) Interesting, yeah I wouldn't expect either of those to work. Why does the increment operator work on an uninitialized variable when other math operators don't?

        1/ no. Although Perl 5.10 has some nice features that it is worth upgrading to for. say and the new switch processing are nice.

        2/ You ought not be doing that in C++ either! C requires declarations to all be at the start of a block, but in C++ you can put em anywhere.

        3/ The increment and update assignment (+=, *=, .=, ...) treat undef as a special case and "do the right thing". Perl has a fair bit of DWIMery (Do What I Mean) and this is one aspect of that.


        True laziness is hard work
        1) I would upgrade to perl 5.10+ for say() alone. Having to write "\n" at the end of a print() statement every time will take years off your life. say() is equivalent to print() with a newline at the end of the output.
Re: how to speed up program dealing with large numbers?
by Solarplight (Sexton) on Mar 22, 2010 at 15:44 UTC

    Well, Thank you everyone for all your help and info. Incase anyone was wondering here is how my code looks now:

    #!/usr/bin/perl use strict; use warnings; my %cache; sub fib { my $n = shift; return $n if $n < 2; return ( $cache{ $n - 1 } ||= fib($n - 1) ) + ( $cache{ $n - 2 } ||= fib($n - 2) ); } print "Fibonnaci Fun\n\n"; print "Pick a number that represents which value of the Fibonnaci sequ +ence you want to start with\n"; print "ie: 1 would be 1, and 2 would be 1, and 3 would be 2 and so on. +\n"; print "Your choice: "; chomp(my $value = <>); print "\nNow pick a number to repesent the range you want.\n"; print "If you want to use values 1 through 10 you you need only enter +10,\n"; print "assuming of course you entered 1 above.\n"; print "Your choice: "; chomp(my $temp = <>); my $range = $value + $temp; my $phiApprox; my $fibValue1; my $fibValue2; for my $i ($value .. $range){ $fibValue1 = fib($i); $fibValue2 = fib($i - 1); $phiApprox = 0 if($i <= 1); $phiApprox = $fibValue1 / $fibValue2 if($i >= 2); print "\n"; print "$fibValue1 / $fibValue2 = $phiApprox"; } print "\n";

      A slight tidy up:

      #!/usr/bin/perl use strict; use warnings; use bignum; print "Fibonnaci Fun\n\n"; print "Pick a number that represents which value of the Fibonnaci sequ +ence you want to start with\n"; print "ie: 1 would be 1, and 2 would be 1, and 3 would be 2 and so on. +\n"; print "Your choice: "; chomp (my $start = <>); print "\nNow pick a number to repesent the range you want.\n"; print "If you want to use values 1 through 10 you you need only enter +10,\n"; print "assuming of course you entered 1 above.\n"; print "Your choice: "; chomp(my $range = <>); print "\n"; for my $fibNum ($start .. $start + $range){ my $fibValue1 = fib($fibNum); my $fibValue2 = fib($fibNum - 1); my $phiApprox = $fibNum <= 1 ? '***' : $fibValue1 / $fibValue2; print "$fibValue1 / $fibValue2 = $phiApprox\n"; } my %cache; sub fib { my ($n) = @_; return $n if $n < 2; --$n; return ($cache{$n} ||= fib($n)) + ($cache{$n - 1} ||= fib($n - 1)) +; }

      Prints (for input numbers 90, 10):

      Fibonnaci Fun Pick a number that represents which value of the Fibonnaci sequence yo +u want to start with ie: 1 would be 1, and 2 would be 1, and 3 would be 2 and so on. Your choice: 90 Now pick a number to repesent the range you want. If you want to use values 1 through 10 you you need only enter 10, assuming of course you entered 1 above. Your choice: 10 2880067194370816120 / 1779979416004714189 = 1.618033988749894848204586 +834365638117579 4660046610375530309 / 2880067194370816120 = 1.618033988749894848204586 +834365638117774 7540113804746346429 / 4660046610375530309 = 1.618033988749894848204586 +8343656381177 12200160415121876738 / 7540113804746346429 = 1.61803398874989484820458 +6834365638117728 19740274219868223167 / 12200160415121876738 = 1.6180339887498948482045 +86834365638117717 31940434634990099905 / 19740274219868223167 = 1.6180339887498948482045 +86834365638117721 51680708854858323072 / 31940434634990099905 = 1.6180339887498948482045 +8683436563811772 83621143489848422977 / 51680708854858323072 = 1.6180339887498948482045 +8683436563811772 135301852344706746049 / 83621143489848422977 = 1.618033988749894848204 +58683436563811772 218922995834555169026 / 135301852344706746049 = 1.61803398874989484820 +458683436563811772 354224848179261915075 / 218922995834555169026 = 1.61803398874989484820 +458683436563811772

      You might have noticed the use of bignum btw.


      True laziness is hard work

        thank you for the tips, one question though

        I don't quite understand this line:

        my $phiApprox = $fibNum <= 1 ? '***' : $fibValue1 / $fibValue2;

        In paticular this part:

         ? '***' :

        Obviously it makes $phiApprox = '***' when $fibNum is <= 1, else it performs the arithmatic. But I don't really understand why, I would never have thought of that code.