in reply to Adding a lot of small integers

First, I see no reason to avoid strict and warnings. The only overhead it adds is the clearing of the variables in each loop pass. If that's an issue, you can avoid the clearing by declaring the vars outside the loops.

IO carries a lot of overhead. Do it all at once. (The python player probably did the same thing, thus the high memory usage.)

#!/usr/bin/perl use strict; use warnings; my (@file, @tr, $prev, $cur, $f, $g); { local $/; @file = split ' ', scalar(<>); } for (1..shift(@file)) { ($prev, @tr) = reverse map [ splice(@file, 0, $_) ], 1..shift(@file); for $cur (@tr) { $g = $prev->[0]; for (0 .. $#$cur) { $cur->[$_] += ($f = $g) > ($g = $prev->[$_ + 1]) ? $f : $g; } $prev = $cur; } print "$prev->[0]\n"; }

And then, let's avoid building an AoA. It saves on building time, memory costs, and avoids the need to deref. Note that splicing/poping/shifting from the ends of an array is very efficient.

#!/usr/bin/perl use strict; use warnings; my (@file, $rows, @tr, @prev, @cur, $f, $g); { local $/; @file = split ' ', scalar(<>); } for (1..shift(@file)) { $rows = shift(@file); @tr = splice(@file, 0, $rows*($rows+1)/2); @prev = splice(@tr, -($rows--)); while ($rows) { @cur = splice(@tr, -($rows--)); $g = $prev[0]; for (0 .. $#cur) { $cur[$_] += ($f = $g) > ($g = $prev[$_ + 1]) ? $f : $g; } @prev = @cur; } print "$prev[0]\n"; }

It might be faster to calculate the index into @tr instead of copying the values into in and out. But maybe not. The hindered readability and the extra additions might cancel out the benefit.

#!/usr/bin/perl use strict; use warnings; my (@file, $rows, @tr, $prev, $cur, $f, $g); { local $/; @file = split ' ', scalar(<>); } for (1..shift(@file)) { $rows = shift(@file); @tr = splice(@file, 0, $rows*($rows+1)/2); $prev = @tr - $rows--; while ($rows) { $cur = $prev - $rows--; $g = $tr[$prev]; for (0 .. $rows) { $tr[$cur + $_] += ($f = $g) > ($g = $tr[$prev + $_ + 1]) ? $f : $g; } $prev = $cur; } print "$tr[0]\n"; }

You could do something similar to avoid creating @tr.

By the way, ($f = $g) > ($g = $prev->[$_ + 1]) is not guaranteed to be evaluated in the order in which you want it to be evaluated. It's technically a bug since operand evaluation order is undefined (although predictable and unlikely to change) in Perl.

Replies are listed 'Best First'.
Re^2: Adding a lot of small integers
by kappa (Chaplain) on Jan 09, 2010 at 23:28 UTC

    Thank you for comprehensive comment, I'm jumping right in to try your suggestions!

    First, I see no reason to avoid strict and warnings. The only overhead it adds is the clearing of the variables in each loop pass. If that's an issue, you can avoid the clearing by declaring the vars outside the loops.

    Adding use strict; and declaring all vars right at the top with our increases my runtime by 5%.

    I didn't know about strict clearing the variables, are you sure this is the reason?

    --kap

      Adding use strict; and declaring all vars right at the top with our increases my runtime by 5%.

      That's surprising, but why would use our?

Re^2: Adding a lot of small integers
by kappa (Chaplain) on Jan 10, 2010 at 00:11 UTC

    I completely eliminated all operations on the structure of data by using indices to a flat @file array all over the place as per your final suggestion and it's the fastest version so far :) Thanks a lot!

    --kap
Re^2: Adding a lot of small integers
by JavaFan (Canon) on Jan 09, 2010 at 23:07 UTC
    First, I see no reason to avoid strict and warnings. The only overhead it adds is the clearing of the variables in each loop pass.
    No, it doesn't. Neither strict nor warnings makes that any variables are cleared. Variables may be cleared if you define them as lexicals inside a loop - but that doesn't change whether you have 'use strict; use warning;' in your program or not.
      I know. Did you read the rest?
        Yes.