D outperformed by C++, what am I doing wrong?
Johan Engelen via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun Aug 13 02:41:39 PDT 2017
On Sunday, 13 August 2017 at 09:08:14 UTC, Petar Kirov
[ZombineDev] wrote:
>
> This instantiation:
>
> sum_subranges(std.range.iota!(int, int).iota(int, int).Result,
> uint)
>
> of the following function:
>
> auto sum_subranges(T)(T input, uint range)
> {
> import std.range : chunks, ElementType, array;
> import std.algorithm : map;
> return input.chunks(range).map!(sum);
> }
>
> gets optimized with LDC to:
> [snip]
> I.e. the compiler turned a O(n) algorithm to O(1), which is
> quite neat. It is also quite surprising to me that it looks
> like even dmd managed to do a similar optimization:
> [snip]
Execution of sum_subranges is already O(1), because the
calculation of the sum is delayed: the return type of the
function is not `uint`, it is `MapResult!(sum, <range>)` which
does a lazy evaluation of the sum.
- Johan
More information about the Digitalmars-d-learn
mailing list