Comment #0 by bearophile_hugs — 2014-02-15T02:53:30Z
The most common code path of std.algorithm.sum contains:
+ Result seed = 0;
+ return reduce!"a + b"(seed, r);
I suggest to add a "static if" that tests if the input is an array (or a random access range) and in such case instead of reduce to use a more efficient loop like this:
immutable lenR = r.length;
for (int i = 2; i < lenR; i += 2) {
total1 += r[i];
total2 += r[i + 1];
}
return total1 + total2;
This is more efficient on most modern CPUs.
Comment #1 by robert.schadek — 2024-12-01T16:20:10Z