in reply to This runs WAY too slow

occasionally some of the results are just plain wrong.

Actually this is your problem No. 1. If the results are sometimes wrong, you can never trust the output. Which means that it doesn't matter how long it takes to get wrong results.

Note, I am using the recommendations from Perl Best Practices.

Surely not all of them. For example you declare way too many variables up front. And you're using many variables with similar names that really belong into a compound data structure (array or hash).

Here's my proposal on how to proceed:

  1. Separate the calculation from any HTML input and CGI output
  2. Write tests for the actual calculations. See Test::Tutorial for a short, gentle introduction
  3. Fix any errors in the calculation
  4. Once you're convinced of the correctness of the calculations, work on the speed. Also feel free to show us the working code and the test suite. I have a few ideas of what might speed up your code, but I wouldn't dare to change anything without a proper test suite.

One of your structural problems with both popnum1 and popnum2 is that it creates a full 2-dimensional array, and then returns a single element. I guess an effort can be made to short-circuit the calculation with knowledge of $x and $y.

There might also be a statistical model that can analytically predict some of the results, and thus avoid a whole lot of calculations, but so far I don't have the time to dig deeper into this.

Replies are listed 'Best First'.
Re^2: This runs WAY too slow
by Dandello (Monk) on Jan 16, 2011 at 15:30 UTC
    Thank you - mostly I needed some fresh eyes. I did some rearranging - calling popfilla (one of those big arrays) from model1 and model2. Now I see some other places to work on and can set up some better testing.