```zsh-users
help / color / mirror / code / Atom feed```
```From: Ray Andrews <rayandrews@eastlink.ca>
To: Zsh Users <zsh-users@zsh.org>
Subject: run time of math problem
Date: Fri, 19 Mar 2021 16:18:13 -0700	[thread overview]

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.

```

```next             reply	other threads:[~2021-03-19 23:18 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-19 23:18 Ray Andrews [this message]
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
```

```Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,

Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

switches of git-send-email(1):

git send-email \
--cc=zsh-users@zsh.org \

https://kernel.org/pub/software/scm/git/docs/git-send-email.html

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