Code:
------
double dot(double[] a, double[] b) {
version(fast) {
assert(a.length == b.length);
double acc = 0.0;
foreach (i; 0 .. a.length)
acc += a[i] * b[i];
return acc;
}
version (slow) {
import std.range : zip;
import std.algorithm.iteration : fold, map;
return zip(a[], b[])
.map!(x => x[0]*x[1])
.fold!((a, b) => a + b)(0.0);
}
}
------
Compiling with manually-written loop:
------
$ time dmd -c -version=fast test.d
real 0m0.021s
user 0m0.014s
sys 0m0.007s
------
Compiling with fancy std.algorithm / std.range templates:
------
real 0m0.499s
user 0m0.444s
sys 0m0.054s
------
Now, I understand that using fancy generic code templates requires more work on the part of the compiler, so some degree of slowdown in compilation times is to be expected.
However, this is an *order of magnitude* slowdown in compiling two functionally-equivalent pieces of code, and very simple code at that. Given our current fast-fast-fast slogan, I think this is unacceptable.
Comment #1 by hsteoh — 2018-02-23T20:06:42Z
Sorry, missed the command for compiling the slow version:
time dmd -c -version=slow test.d
Comment #2 by robert.schadek — 2024-12-13T18:57:21Z