Reduce has dreadful performance?
Laeeth Isharc via Digitalmars-d
digitalmars-d at puremagic.com
Thu Jun 18 03:45:16 PDT 2015
On Thursday, 18 June 2015 at 10:34:46 UTC, Laeeth Isharc 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;
>> }
>>
>> runs in about 6.70s. However the code:
>>
>> double sequential_reduce(const int n, const double delta) {
>> return 4.0 * delta * reduce!((double t, int i){immutable x =
>> (i -
>> 0.5) * delta; return t + 1.0 / (1.0 + x * x);})(0.0, iota(1, n
>> + 1));
>> }
>>
>> runs in about 17.03s, whilst:
>>
>> double sequential_reduce_alt(const int n, const double delta) {
>> return 4.0 * delta * reduce!"a + b"(
>> map!((int i){immutable x = (i - 0.5) * delta; return
>> 1.0 /
>> (1.0 + x * x);})(iota(1, n + 1)));
>> }
>>
>> takes about 28.02s. Unless I am missing something (very
>> possible), this
>> is not going to create a good advert for D as an imperative
>> language
>> with declarative (internal iteration) expression.
>
> which compiler ?
fwiw n=1bn, delta=0.1
ldc no params:
8.749s/17.466s
ldc -O2
4.541s/4.562s
gdc no params
4.690/22.163 (!)
gdc -O2
4.552/4.551
More information about the Digitalmars-d
mailing list