D and Nim
weaselcat via Digitalmars-d
digitalmars-d at puremagic.com
Sun Jan 4 17:07:06 PST 2015
On Monday, 5 January 2015 at 00:54:45 UTC, Walter Bright wrote:
> On 1/4/2015 1:46 PM, Ary Borenszweig wrote:
>> Nim is faster than D.
>
> I bet the D version would be significantly faster if you didn't
> read the file as a text string, then try breaking it up into an
> array of lines, then break those lines up into an array of
> strings, etc. This is a lot of memory allocation.
>
> Instead, open the file and use byLine() on it, which does not
> allocate memory.
It doesn't time that part
Either way I sped it up by changing the non-fast(?) part a little
bit
int getLongestPath(immutable(node[]) nodes, const int nodeID,
bool[] visited) nothrow @safe{
visited[nodeID] = true;
scope(exit) visited[nodeID] = false;
return reduce!(max)(0,
nodes[nodeID].neighbours
.filter!(x=>!visited[x.dest])
.map!(x => x.cost + getLongestPath(nodes, x.dest,
visited)));
}
I'm not that great at D though.
Compiled it with LDC, GDC and DMD seem to choke on the functional
programming and run 4-5x slower.
~1120 ms with my version, ~1210 ms with the 'fast' version
the C++ version compiled with gcc still beats it by about 100ms
on my machine, but it's the same exact performance as C++ when
using clang/LLVM.
Why does reduce! take the seed as its first parameter btw? It
sort of messes up function chaining.
More information about the Digitalmars-d
mailing list