Why is this code slow?

Sergey kornburn at yandex.ru
Sun Mar 24 21:15:21 UTC 2024


On Sunday, 24 March 2024 at 19:31:19 UTC, Csaba wrote:
> As you can see the function that does the job is exactly the 
> same in C and D.
Not really..

The speed of Leibniz algo is mostly the same. You can check the 
code in this benchmark for example: 
https://github.com/niklas-heer/speed-comparison

What you could fix in your code:
* you can use enum for BENCHMARKS and ITERATIONS
* use pow from core.stdc.math
* use sw.reset() in a loop

So the main part could look like this:
```d
auto sw = StopWatch(AutoStart.no);
sw.start();
foreach (i; 0..BENCHMARKS) {
     result += leibniz(ITERATIONS);
     total_time += sw.peek.total!"nsecs";
     sw.reset();
}
sw.stop();
```


More information about the Digitalmars-d-learn mailing list