http://d.puremagic.com/issues/show_bug.cgi?id=4725



--- Comment #2 from bearophile_h...@eml.cc 2010-12-12 23:41:50 PST ---
sum() may be implemented better than just using reduce() because a smarter
sum() may keep inside two variables and sum them in parallel each loop. And
then sum the two partial sums at the end and return them. Experiments (and
theory) show that on modern CPUs this is more efficient than a normal loop with
a single accumulation variable.

I mean code like (specialized for random access ranges, that's a very common
case worth specializing for, because sum() is a very common operation that
needs to be fast):


switch (array.length) {
    case 0:
        break;
    case 1:
        total = array[0];
        break;
    default:
        total = array[0];
        auto total2 = cast(typeof(total))array[1];
        int stop = array.length & (~1);
        for (int i = 2; i < stop; i += 2) {
            total += array[i];
            total2 += array[i + 1];
        }
        total += (array.length % 2 ? (total2 + array[$-1]) : total2);
        break;
}
// return total here

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------

Reply via email to