On 20.09.2013 17:28, Andrei Alexandrescu wrote:
On 9/20/13 2:36 AM, bearophile wrote:
In D it's often better to use std.algorithm/range instead of raw foreach
loops (despite probably unlike C++ in D a heavy use of ranges leads to a
lower performance, even if you use the LDC2 compiler).

Why would there be a performance loss?

Because range concept underutilize current CPUs. I don't know if all empty/front/popFront are inlined. I suppose some templated functions are, but surely not all of them. What will be faster, calling all of those functions for each element or traversing memory using a loop?

Of course 2nd option will be faster, because the act of traversing (cached) memory does not have the overhead of a function call.

Some time ago I proposed a hybrid range concept where the front() is an array. All algorithms would first traverse the memory in this array, then call popFront to get another array (slice). In this way, function call overhead is minimized because it's paid sparsely rather than for each range element.

Reply via email to