Bug 12175 – More efficient very common case for std.algorithm.sum

Status
NEW
Severity
enhancement
Priority
P4
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-02-15T02:53:30Z
Last change time
2024-12-01T16:20:10Z
Assigned to
No Owner
Creator
bearophile_hugs
Moved to GitHub: phobos#9625 →

Comments

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
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/phobos/issues/9625 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB