fork download
  1. import std.stdio : writeln;
  2. import std.algorithm.comparison: min, equal;
  3. import std.algorithm.iteration: sum;
  4. import core.time: MonoTime, Duration;
  5.  
  6.  
  7. auto sum_subranges(T)(T input, uint range)
  8. {
  9. import std.range : chunks;
  10. import std.algorithm : map;
  11.  
  12. if (range != 0)
  13. {
  14. return input.chunks(range).map!(sum);
  15. }
  16. return typeof(return).init;
  17. }
  18.  
  19. void main()
  20. {
  21. import std.range : iota, array;
  22. auto v = iota(0,1000000).array;
  23. int sum;
  24. MonoTime beg = MonoTime.currTime;
  25. for (int i=0; i < 100; i++)
  26. sum += cast(int)sum_subranges(v,2).length;
  27. MonoTime end = MonoTime.currTime;
  28. writeln(end-beg);
  29. writeln(sum);
  30.  
  31. assert(equal(sum_subranges([1,1,1], 2), [2, 1]));
  32. assert(equal(sum_subranges([1,1,1,2,3,3], 2), [2, 3, 6]));
  33. assert(equal(sum_subranges(cast(int[])[], 2), cast(int[])[]));
  34. assert(equal(sum_subranges([1], 2), [1]));
  35. }
Success #stdin #stdout 0.02s 17352KB
stdin
Standard input is empty
stdout
4 μs and 1 hnsec
50000000