Recently, I start to use dlang in competitive programming. There are 3 dlang compiler, "DMD", "LDC", "GDC".
In competitive programming, best choice is "LDC", but in many online judge, we can use only "DMD" or "GDC".
I hope to many online judge install "LDC", so I write this blog.
Compiler compare
- DMD : Official compiler. Can use latest language version. Moderate Fast.
- LDC : based on llvm. Moderate new. Very Fast.
- GDC : based on gcc. Not new, Moderate Fast.
How to install ldc
- Go to github's release page https://github.com/ldc-developers/ldc/releases
- Download ldc(If you use 64bit-linux,
ldc2-1.2.0-linux-x86_64.tar.xz
, in now) - Extract
- ADD
/your/download/path/ldc2-1.2.0-linux-x86_64/bin
to $PATH.
How to use ldc
Debug mode :
ldc2 -d-debug A.d
Release mode(and optimize) :
ldc2 -O -release A.d
LDC is indeed nice, it's almost bleeding edge in terms of language features, and makes fast executables. The main advantage of DMD is that it compiles fast, which matters when compiling locally. On the other hand, the compiled executable may perform sub-optimally compared to when using LLVM and GCC backends.
Anyway, in my experience at setting and participating in programming contests, D compiled with DMD almost always performs between C++/GCC and Java if we look at similar solutions in these languages. And since the time limit is usually at least 2x Java time, DMD is a viable, if not the most optimal, choice.
Since I mainly use DMD, my command lines differ:
(release)
dmd -O -release -inline -boundscheck=off
(debug)
dmd -g -debug -unittest
Here,
-debug
is useful for debug output. The following program can be debugged locally and then submitted without any changes:And
-g
keeps the stack trace meaningful when something goes wrong.On Windows, I have to add
-L/STACK:268435456
to have enough stack available, similar to mingw/gcc.Also, there's
ldmd2
executable in LDC which accepts options in the same form asdmd
and provides an easy DMD-to-LDC transition.I edited ldc's debug version flag, thanks :) I use stupid debug in usually(comment out all debug writeln before submit). I would use debug{} in future.
Auto comment: topic has been updated by yosupo (previous revision, new revision, compare).
On SPOJ LDC2-1.1.0 is a little bit slower than DMD-2.072 (for several problems and same code I got TL for LDC2 and AC for DMD). It is very strange, because when I installed same versions of both compilers at home computer, sources compiled by LDC2 are executed about 1.5 times faster than compiled by DMD.
I guess this is caused by the compilier option, but I can't find compilier option of SPOJ
I have implemented a D compiler configuration sanity checker program when investigating a rather abnormal GDC compiler performance on codechef. Running the same checker program via SPOJ customtest confirms that SPOJ also doesn't have any optimizations enabled for their LDC and GDC compilers even now:
So no improvements in 3 years. I'm not a real SPOJ user myself, but maybe somebody could try to contact them? If no action is taken, then their compilers will remain misconfigured indefinitely.
Are you curious to try it or there are some advantages of Dlang over C/C++?
I've assembled some answer here.
thank you Master Gassa for such detailed post cum answer.