D outperformed by C++, what am I doing wrong?
Neia Neutuladh via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun Aug 13 00:03:46 PDT 2017
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.
More information about the Digitalmars-d-learn
mailing list