Why is D slower than LuaJIT?

spir denis.spir at gmail.com
Thu Dec 23 05:04:51 PST 2010


On Wed, 22 Dec 2010 22:14:34 -0600
Andrei Alexandrescu <SeeWebsiteForEmail at erdani.org> wrote:

> I then replaced iota's implementation with a simpler one that's a 
> forward range. Then the performance became exactly the same as for the 
> simple loop.


After having watched Iota's very general implementation, I tried the same change, precisely. Actually, with an even simpler range requiring a single element type for (first,last,step). For any reason, this alternative is slightly slower by me than using Iota (don't cry watching absolute times, my computer is old and slow ;-). Sample code below, typical results are:

1.1 3.3 5.5 7.7 
Interval time: 1149
Iota time: 1066

Note: adding an assert to ensure front or popfront is not wrongly called past the end adds ~ 20% time.
Note: I think this demonstates that using Iota does not perform undue computations (multiplication to get Nth element with multiplication + addition), or do I misinterpret?

Anyway, what is wrong in my code? What doesn't it perform better?


import std.algorithm    : map, filter, reduce;
import std.range        : iota;
struct Interval (T) {
    alias T Element;
    Element first, last, step;
    private Element element;
    this (Element first, Element last, Element step=1) {
        this.first = first;
        this.last = last;
        this.step = step;
        this.element = first;
    }
    @property void popFront () {
        this.element += this.step;
    }
    @property bool empty () {
        return (this.element > this.last);
    }
    @property Element front () {
        return this.element;
    }
}
void main () {
    auto nums = Interval!float(1.1,8.8, 2.2);
    foreach(n ; nums) writef("%s ", n);
    writeln();

    auto t1 = time();
    auto nums1 = Interval!int(0, 10_000_000);
    auto halves1 = map!"a/2"(nums1);
    auto incs1 = map!"a+2"(halves1);
    auto result1 = reduce!"a+b"(incs1);
    writefln("Interval time: %s", time() - t1);
    
    auto t2 = time();
    auto nums2 = iota(0, 10_000_000);
    auto halves2 = map!"a/2"(nums2);
    auto incs2 = map!"a+2"(halves2);
    auto result2 = reduce!"a+b"(incs2);
    writefln("Iota time: %s", time() - t2);
}


Denis
-- -- -- -- -- -- --
vit esse estrany ☣

spir.wikidot.com



More information about the Digitalmars-d mailing list