in reply to This runs WAY too slow

Welcome to Perl Monks!

If I am reading your program right, your program is running on the order of 0(N-squared) where N=$total, i.e. the cumulative grand total of all population years and then some. It definitely isn't going to scale well.

The culprit I suspect is the way you are calculating your random variations. Each of those calculations runs $total*$gener loops, where $total is the cumulative grand total for all years and then some and $gener is the largest gap in years. You have at least three routines doing this popfilea(), popnum1() and popnum2(). At least one of these mega loops appears to be called for each and every cell of your table. Since one of the table dimensions is derived from the grand total, you are going to end up in O(N^2) land. However, to be sure of the real cause, you should use a profiler, as was suggested above.

There are some additional minor efficiency issues. As these involve very small numbers of elements, I doubt they are the source of your problems. Still they are habits to watch out for:

As for reducing the complexity of your functions: move all those $fooN variables into arrays, e.g. @mpe, @mpy, @grand etc. Then you can do a simply loop to set things up instead of all those repetitive almost alike lines.

Finally, this code was very hard to read largely because it was hard to tell which variables went with which processing steps. The following tips might remedy this:

Something like this:

# $hFORM is a hash reference my $hFORM = parseCGIStream(\*STDIN); # input is stored as key-value pairs in $hFORM # output is all these variables my ($aMpe, $aMpy, $aGrand, $aPopbyyer , $total, $gener) = datafilla($hFORM); # input is $gener, $total # output is $aOa (array reference storing your @aoa) my $aOa = popfilea($gener, $total); #... and so on ...