I wonder how fast we'd do

Marco de Wild mdwild at sogyo.nl
Tue May 28 05:54:07 UTC 2019


On Tuesday, 28 May 2019 at 05:20:14 UTC, Uknown wrote:
> On Tuesday, 28 May 2019 at 04:38:32 UTC, Andrei Alexandrescu 
> wrote:
>> https://jackmott.github.io/programming/2016/07/22/making-obvious-fast.html
>
> I tested 3 D variants :
>
> ---ver1.d
> double sum = 0.0;
> for (int i = 0; i < values.length; i++)
> {
> 	double v = values[i] * values[i];
> 	sum += v;
> }
>
>
> ---ver2.d
> double sum = 0.0;
> foreach (v; values)
>         sum += v * v;
> return sum;
>
>
> ---ver3.d
> import std.algorithm : sum;
> double[] squares;
> squares[] = values[] * values[];
> return squares.sum;
>
> All 3 were the exact same with LDC. 
> https://run.dlang.io/is/6pjEud

When the blog post released I wrote a few benchmarks. 
Surprisingly, using

values.map!(x => x*x).sum

was the fastest (faster than v1). It got around to 20 us on my 
machine.



More information about the Digitalmars-d mailing list