Reduce has dreadful performance?

Andrea Fontana via Digitalmars-d digitalmars-d at puremagic.com
Thu Jun 18 03:50:08 PDT 2015


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;
}

?


More information about the Digitalmars-d mailing list