Re: Why is this code slow?

2024-03-26 Thread Csaba via Digitalmars-d-learn
On Sunday, 24 March 2024 at 21:21:13 UTC, kdevel wrote: Usually you do not translate mathematical expressions directly into code: ``` n += pow(-1.0, i - 1.0) / (i * 2.0 - 1.0); ``` The term containing the `pow` invocation computes the alternating sequence -1, 1, -1, ..., which can be rep

Why is this code slow?

2024-03-24 Thread Csaba via Digitalmars-d-learn
I know that benchmarks are always controversial and depend on a lot of factors. So far, I read that D performs very well in benchmarks, as well, if not better, as C. I wrote a little program that approximates PI using the Leibniz formula. I implemented the same thing in C, D and Python, all of