zsh-users
 help / color / mirror / code / Atom feed
* run time of math problem
@ 2021-03-19 23:18 Ray Andrews
  2021-03-19 23:46 ` Lawrence Velázquez
  2021-03-19 23:52 ` Oliver Kiddle
  0 siblings, 2 replies; 6+ messages in thread
From: Ray Andrews @ 2021-03-19 23:18 UTC (permalink / raw)
  To: Zsh Users

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.





^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-03-20  4:00 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-19 23:18 run time of math problem Ray Andrews
2021-03-19 23:46 ` Lawrence Velázquez
2021-03-19 23:51   ` Ray Andrews
2021-03-19 23:52 ` Oliver Kiddle
2021-03-20  2:40   ` Ray Andrews
2021-03-20  3:59     ` Lawrence Velázquez

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).