xxxxxxxxxx
import std.stdio;
import std.conv : to;
import std.datetime.stopwatch;
auto first = MySpecialType('c');
auto second = "string";
void main()
{
auto r = benchmark!(f0, f1)(10_000_000);
Duration f0Result = r[0];
Duration f1Result = r[1];
writeln("f0Result: ", f0Result);
writeln("f1Result: ", f1Result);
}
void f0()
{
foo1(first, second);
}
void f1()
{
foo2(first, second);
}
struct MySpecialType { char c; }
auto foo1(Args...)(Args args)
{
MySpecialType[] bar;
foreach(ref arg; args)
{
static if(is(typeof(arg) == MySpecialType))
{
bar ~= arg;
}
else
{
foreach(c; to!string(arg))
{
bar ~= MySpecialType(c);
}
}
}
assert(bar.length == 7);
// do more stuff
}
auto foo2(Args...)(Args args)
{
MySpecialType[] bar;
size_t counter;
size_t current;
foreach(ref arg; args)
{
static if(is(typeof(arg) == MySpecialType))
{
++counter;
}
else
{
import std.conv : to;
counter += to!string(arg).length;
}
}
bar.length = counter;
foreach(ref arg; args)
{
static if(is(typeof(arg) == MySpecialType))
{
bar[current] = arg;
++current;
}
else
{
import std.conv : to;
foreach(c; to!string(arg))
{
bar[current] = MySpecialType(c);
++current;
}
}
}
assert(bar.length == 7);
// do more stuff
}