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



--- Comment #4 from Denis Derman <denis.s...@gmail.com> 2010-12-24 11:23:28 PST 
---
(In reply to comment #2)
> 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

You should add a 'case 2:' to avoid complication of a simple and common case.
Maybe also avoid // computation under a given number of elements (to be
defined).

denis

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

Reply via email to