[Novalug] [OT] Question for computer scientist

Richard Rognlie rrognlie@gamerz.net
Mon Feb 2 16:58:51 EST 2009


"It Depends"

Sorry, but that's as good as you're going to get right off.

However, that said...

does the function (e.g. the powers of 2) use a significant 
number of cycles?  if so, calculate it once, and look up may be a win.

then the question is... does it make sense to pre-calculate all of the
possible values?  or it is better to treat it like a cache?


One way to test the performance is with a simple harness and then use the
"time" program to see how much system vs. user time is involved in
performing the program one way vs. the other.


for example, if you have N function values to calculate.

run the program M times (1 interation... 2 interations... ... M iterations)


style 1

for i = 1 .. M
        for j = 1 .. N
                calculate f(j)
        end
end



style 2

for j = 1 .. N
        X[j] = f(j)
end

for i = 1 .. M
        for j = 1 .. N
                lookup X[j]
        end
end



in a non-precalculated version (so you don't waste the cycles calculating
values for f(j) that you don't ever use...   the function f would include
a static table of already calculated values


and the function would look something like

function f (int j)
{
        static X[];

        if (X[j] == undef)
                X[j] = calculate (j);
        end

        return X[j];
}



I've intentionally left the code pseudo-codish... because you did not specify
which language you're writing for.
        

It may well have been that... at the time, the DOS program was sufficiently
slow that making it a lookup helped.  But these days, you may not even
notice the delta.

using the hybrid function might be the easiest way to have the best of 
both worlds without going through the expense of a full blown performance
analysis.

Regards,

Richard




On Mon, Feb 02, 2009 at 04:44:56PM -0500, Bonnie Dalzell wrote:
> If you have a value you are using a lot in a program - in this case 
> powers of 2 (2^n and 1/2 to the n). Is it easier on the system (or 
> faster) to compute them when you need them or fetch them from a 
> premade look up table?
> 
> I ask this because I am working on a program to compute coefficients of 
> inbreeding and the closed source compiled dos program I have been using 
> seems to have a look up table file that it uses in relation to coi and I 
> think - examining the table in a hex editor that it is powers of two 
> precalculated.
> 
> I do not have access to the source code for the old dos program. but i 
> have been running it to check and see if the calcs of the value 
> generated by my program are correct.
> 
> No one seems to have published a numerical recipe for calculating 
> coefficient of inbreeding anyplace i can find via google so I (a 
> non-mathematician) am trying to come up with one.
> 
> I have also decided if I get one (numerical recipe) that works it is going 
> onto the web as is the program. Since the program is in perl it is by 
> defiition open source.
> 
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>                         Bonnie Dalzell, MA
> mail:5100 Hydes Rd PO Box 60, Hydes,MD,USA 21082-0060|EMAIL:bdalzell@qis.net
> 
> freelance anatomist, vertebrate paleontologist, writer, illustrator, dog
> breeder, computer nerd & iconoclast... Borzoi info at www.borzois.com.
> Editor Net.Pet Online Animal Magazine  - http://www.netpetmagazine.com
> HOME http://www.qis.net/~borzoi/          BUSINESS http://www.batw.com
> _______________________________________________
> Novalug mailing list
> Novalug@calypso.tux.org
> http://calypso.tux.org/cgi-bin/mailman/listinfo/novalug

-- 
 /  \__  | Richard Rognlie / Sendmail Ninja / Gamerz.NET Lackey
 \__/  \ | http://www.gamerz.net/~rrognlie    <rrognlie at gamerz.net>
 /  \__/ | Creator of pbmserv@gamerz.net
 \__/    |                Helping reduce world productivity since 1994



More information about the Novalug mailing list