Look and think good things about D
bearophile
bearophileHUGS at lycos.com
Fri Nov 15 05:33:32 PST 2013
Ary Borenszweig:
> And so LLVM, which is what Crystal uses as a backend.
LDC2 uses the same back end :-)
> But I thought ranges were meant to be fast. No allocations and
> all of that. In fact, I was kind of sad that Crystal doesn't
> have a similar concept so it could never get as fast as D
> ranges. But if D ranges are not fast, what's the point of
> having them and making everyone use them?
If you use ranges badly you will get a slow program, if you use
them well with a good back-end, you will have a fast program.
I have written a third intermediate program, it's longer than
yours, and it seems much slower than your code:
void main(in string[] args) {
import std.stdio, std.conv, std.algorithm, std.array;
immutable n = (args.length == 2) ? args[1].to!uint : 10;
if (n == 0)
return;
auto seq = "1";
writefln("%2d: n. digits: %d", 1, seq.length);
foreach (immutable i; 2 .. n + 1) {
Appender!string result;
foreach (immutable digit, immutable count; seq.group) {
result ~= "123"[count - 1];
result ~= digit;
}
seq = result.data;
writefln("%2d: n. digits: %d", i, seq.length);
}
}
On my system it runs in 0.34 seconds for n=50. Could you compare
some of the timings of the various D/Crystal versions on your
system (using ldc2 for D)?
Bye,
bearophile
More information about the Digitalmars-d
mailing list