I wonder how fast we'd do

Marco de Wild mdwild at sogyo.nl
Tue May 28 08:55:18 UTC 2019


On Tuesday, 28 May 2019 at 05:54:07 UTC, Marco de Wild wrote:
> 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.

Should have been 20 ms of course.

https://run.dlang.io/is/Fpg8Iw

21 ms, 387 μs, and 7 hnsecs (map)
32 ms, 191 μs, and 1 hnsec (foreach)
32 ms, 183 μs, and 8 hnsecs (for)

However, recompiling it with LDC (to reproduce the exact compile 
flags) gives exactly the opposite result *facepalm*, bumping the 
map to 40 ms:

41 ms, 792 μs, and 7 hnsecs
30 ms and 893 μs
31 ms, 76 μs, and 6 hnsecs


More information about the Digitalmars-d mailing list