On Sunday, 13 August 2017 at 06:09:39 UTC, amfvcg wrote:
Hi all,
I'm solving below task:

Well, for one thing, you are preallocating in C++ code but not in D.

On my machine, your version of the code completes in 3.175 seconds. Changing it a little reduces it to 0.420s:

    T[] result = new T[input.length];
    size_t o = 0;
    for (uint i; i < input.length; i=min(i+range, input.length))
    {
        result[o] = sum(input[i..min(i+range, input.length)]);
        o++;
    }
    return result[0..o];

You can also use Appender from std.array.

Reply via email to