xxxxxxxxxx
double base_c(double[] values) pure nothrow
{
double sum = 0.0;
for (int i = 0; i < values.length; i++)
{
double v = values[i] * values[i];
sum += v;
}
return sum;
}
double base_d(double[] values) pure nothrow
{
double sum = 0.0;
foreach (v; values)
sum += v * v;
return sum;
}
double d_with_sum(double[] values) pure nothrow
{
import std.algorithm : sum;
double[] squares;
squares[] = values[] * values[];
return squares.sum;
}
void main()
{
import std.random : uniform, Random, unpredictableSeed;
import std.stdio : writeln;
import std.datetime.stopwatch : StopWatch, Duration;
double[] values;
auto rnd = Random(unpredictableSeed);
foreach (i; 0 .. 32_000_000)
values ~= uniform(0.0f, 10.0, rnd);
Duration t1, t2, t3;
StopWatch sw;
enum n = 100_000;
foreach (_; 0 .. n) {
sw.reset();
sw.start();
auto r = base_c(values);
sw.stop();
t1 += sw.peek();
sw.reset();
sw.start();
r = base_d(values);
sw.stop();
t2 += sw.peek();
sw.reset();
sw.start();
r = d_with_sum(values);
sw.stop();
t3 += sw.peek();
}
writeln("t1 : ", t1 / n);
writeln("t2 : ", t2 / n);
writeln("t3 : ", t3 / n);
}