module count;
import std.algorithm;
import std.array;
import std.stdio;
import std.range;
import std.traits;
import std.datetime;
alias std.array.replicate replicate;
void main()
{
auto arr = array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10].replicate(100));
auto r = benchmark!({splitLengthOld(arr, 2);}, {splitLengthNew(arr, 2);})(10_000);
writefln("splitLengthOld 10_000 times: %s msecs.", r[0].to!("msecs", int));
writefln("splitLengthNew 10_000 times: %s msecs.", r[1].to!("msecs", int));
}
T[] splitLengthOld(T)(T arr, size_t count) if (isArray!T)
{
T[] result;
while (arr.length)
{
result ~= arr.take(count);
arr.popFrontN(count);
}
return result;
}
T[] splitLengthNew(T)(T arr, size_t count) if (isArray!T)
{
T[] result;
assert(count > 0);
result.reserve((arr.length + count - 1) / count);
while (arr.length > count)
{
result ~= arr[0..count];
arr = arr[count..$];
}
if (arr.length)
result ~= arr;
return result;
}