Reduce has dreadful performance?

Ilya Yaroshenko via Digitalmars-d digitalmars-d at puremagic.com
Thu Jun 18 03:52:43 PDT 2015


On Thursday, 18 June 2015 at 10:50:09 UTC, Andrea Fontana wrote:
> On Thursday, 18 June 2015 at 10:46:18 UTC, Ilya Yaroshenko 
> wrote:
>> On Thursday, 18 June 2015 at 10:27:58 UTC, Russel Winder wrote:
>>> On a given machine, the code:
>>>
>>> double sequential_loop(const int n, const double delta) {
>>>   auto sum = 0.0;
>>>   foreach (immutable i; 1 .. n + 1) {
>>>     immutable x = (i - 0.5) * delta;
>>>     sum += 1.0 / (1.0 + x * x);
>>>   }
>>>   return 4.0 * delta * sum;
>>> }
>
> What about this:
>
> double sequential_alternative(const int n, const double delta) {
> 	return 4.0 * delta * iota(1, n+1).map!(x => 
> (x-0.5)*delta).map!(x => 1.0/(1.0 + x*x)).sum;
> }
>
> ?

This is not equivalent, because sum uses another (more precise) 
algorithms to do summation. It should work a little bit slower.


More information about the Digitalmars-d mailing list