in reply to how to speed up program dealing with large numbers?

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

Replies are listed 'Best First'.
Re^2: how to speed up program dealing with large numbers?
by Solarplight (Sexton) on Mar 22, 2010 at 04:25 UTC

    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) Cool, I'll run a portupgade on perl, thanks.

        2) Really, I had been taught that it was at least convention, if not necessary, and I have seen others C++ code that does that.

        3) Interesting, good to know. Thats really cool.

        And again, thanks for all the great info.

      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.
        Except that you spend those years anyway reading programs that alternate between say and print depending on whether they're printing full lines of output at a time.