<div dir="ltr">Here is more D idiomatic way:<div><br></div><div><div>import std.stdio : writeln;</div><div>import std.algorithm.comparison: min;</div><div>import std.algorithm.iteration: sum;</div><div>import core.time: MonoTime, Duration;</div><div><br></div><div><br></div><div>auto sum_subranges(T)(T input, uint range)</div><div>{</div><div> import std.array : array;</div><div> import std.range : chunks, ElementType;</div><div> import std.algorithm : map;</div><div><br></div><div> if (range == 0)</div><div> {</div><div> return ElementType!(T)[].init;</div><div> }</div><div> return input.chunks(range).map!(sum).array;</div><div>}</div><div><br></div><div>unittest</div><div>{</div><div> assert(sum_subranges([1,1,1], 2) == [2, 1]);</div><div> assert(sum_subranges([1,1,1,2,3,3], 2) == [2, 3, 6]);</div><div> assert(sum_subranges([], 2) == []);</div><div> assert(sum_subranges([1], 2) == [1]);</div><div> assert(sum_subranges([1], 0) == []);</div><div>}</div><div><br></div><div><br></div><div>int main()</div><div>{</div><div> import std.range : iota, array;</div><div> auto v = iota(0,1000000);</div><div> int sum;</div><div> MonoTime beg = MonoTime.currTime;</div><div> for (int i=0; i < 100; i++)</div><div> sum += cast(int)sum_subranges(v,2).length;</div><div> MonoTime end = MonoTime.currTime;</div><div> writeln(end-beg);</div><div> writeln(sum);</div><div> return sum;</div><div>}</div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Aug 13, 2017 at 9:13 AM, Daniel Kozak <span dir="ltr"><<a href="mailto:kozzi11@gmail.com" target="_blank">kozzi11@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>this works ok for me with ldc compiler, gdc does not work on my arch machine so I can not do comparsion to your c++ versin (clang does not work with your c++ code)</div><span class=""><div><br></div><div>import std.stdio : writeln;</div><div>import std.algorithm.comparison: min;</div><div>import std.algorithm.iteration: sum;</div><div>import core.time: MonoTime, Duration;</div><div><br></div><div><br></div><div>T[] sum_subranges(T)(T[] input, uint range)</div><div>{</div></span><div> import std.array : appender;</div><div> auto app = appender!(T[])();</div><div> if (range == 0)</div><div> {</div><div> return app.data;</div><span class=""><div> }</div><div> for (uint i; i < input.length; i=min(i+range, input.length))</div><div> {</div></span><div> app.put(sum(input[i..min(i+<wbr>range, input.length)]));</div><div> }</div><div> return app.data;</div><span class=""><div>}</div><div><br></div><div>unittest</div><div>{</div><div> assert(sum_subranges([1,1,1], 2) == [2, 1]);</div><div> assert(sum_subranges([1,1,1,2,<wbr>3,3], 2) == [2, 3, 6]);</div><div> assert(sum_subranges([], 2) == []);</div><div> assert(sum_subranges([1], 2) == [1]);</div><div> assert(sum_subranges([1], 0) == []);</div><div>}</div><div><br></div><div><br></div><div>int main()</div><div>{</div></span><div> import std.range : iota, array;</div><div> auto v = iota(0,1000000).array;</div><span class=""><div> int sum;</div><div> MonoTime beg = MonoTime.currTime;</div><div> for (int i=0; i < 100; i++)</div><div> sum += cast(int)sum_subranges(v,2).<wbr>length;</div><div> MonoTime end = MonoTime.currTime;</div><div> writeln(end-beg);</div><div> writeln(sum);</div><div> return sum;</div><div>}</div></span></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Aug 13, 2017 at 9:03 AM, Neia Neutuladh via Digitalmars-d-learn <span dir="ltr"><<a href="mailto:digitalmars-d-learn@puremagic.com" target="_blank">digitalmars-d-learn@<wbr>puremagic.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span>On Sunday, 13 August 2017 at 06:09:39 UTC, amfvcg wrote:<br>
</span><span><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi all,<br>
I'm solving below task:<br>
</blockquote>
<br></span>
Well, for one thing, you are preallocating in C++ code but not in D.<br>
<br>
On my machine, your version of the code completes in 3.175 seconds. Changing it a little reduces it to 0.420s:<br>
<br>
T[] result = new T[input.length];<br>
size_t o = 0;<span><br>
for (uint i; i < input.length; i=min(i+range, input.length))<br>
{<br></span>
result[o] = sum(input[i..min(i+range, input.length)]);<br>
o++;<br>
}<br>
return result[0..o];<br>
<br>
You can also use Appender from std.array.<br>
</blockquote></div><br></div>
</div></div></blockquote></div><br></div>