Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

run time of math problem



This question might not even be answerable and it's only a point of interest:
I was working out some probabilities and the algorithm looks like this:

   for ((level=1; level<100; level++)); do
        sum=
        remainder=$(( (level - 1.0) / level ))
        for ((terms=level; terms; terms--)); do
            sum+=$(( remainder**(terms - 1) ))
        done
        divided=$(( sum * (1.0 / level) ))
        echo for level: $level, survival: $divided
   done

Now if I avoid the 'remainder' variable:

    for ((level=1; level<100; level++)); do
        sum=
        for ((terms=level; terms; terms--)); do

            # 'remainder' calculation done directly here:
            sum+=$(( ( (level - 1.0) / level )**(terms - 1) ))
        done
        divided=$(( sum * (1.0 / level) ))
        echo for level: $level, survival: $divided
   done

... I'd expect the thing to run a teeny bit faster but in fact it
runs about 15% slower.  Is that explicable?  Does zsh prefer
calculations done in steps with variables for each step?  Or is this
just some little anomaly?  Things might not optimize every time.







Messages sorted by: Reverse Date, Date, Thread, Author