Why is this code slow?
Salih Dincer
salihdb at hotmail.com
Thu Mar 28 01:09:34 UTC 2024
On Wednesday, 27 March 2024 at 08:22:42 UTC, rkompass wrote:
> I apologize for digressing a little bit further - just to share
> insights to other learners.
Good thing you're digressing; I am 45 years old and I still
cannot say that I am finished as a student! For me this is
version 4 and it looks like we don't need a 3rd variable other
than the function parameter and return value:
```d
auto leibniz_v4(int i) @nogc pure {
double n = 0.5*((i%2) ? -1.0 : 1.0) / (i * 2.0 + 1.0);
while(--i >= 0)
n += ((i%2) ? -1.0 : 1.0) / (i * 2.0 + 1.0);
return n * 4.0;
} /*
3.1415926535892931
3.141592653589 793238462643383279502884197169399375105
3.141593653590774200000 (v1)
Avg execution time: 0.000033
*/
```
SDB at 79
More information about the Digitalmars-d-learn
mailing list