gdc or ldc for faster programs?

H. S. Teoh hsteoh at quickfur.ath.cx
Tue Jan 25 20:42:53 UTC 2022


On Tue, Jan 25, 2022 at 11:52:17AM -0800, Ali Çehreli via Digitalmars-d-learn wrote:
> Sorry for being vague and not giving the code here but a program I
> wrote about spelling-out parts of a number (in Turkish) as in "1
> milyon 42" runs much faster with gdc.
> 
> The program integer-divides the number in a loop to find quotients and
> adds the word next to it. One obvious optimization might be to use
> POSIX div() and friends to get the quotient and the remainder at one
> shot but I made myself believe that the compilers already do that.
> (But still not sure. :o))

Don't guess at what the compilers are doing; disassemble the binary and
see for yourself exactly what the difference is. Use run.dlang.io for a
convenient interface that shows you exactly how the compilers translated
your code. Or if you're macho, use `objdump -d` and search for _Dmain
(or the specific function if you know how it's mangled).


> I am not experienced with dub but I used --build=release-nobounds and
> verified that -O3 is used for both compilers. (I also tried building
> manually with GNU 'make' with e.g. -O5 and the results were similar.)
> 
> For a test run for 2 million numbers:
> 
> ldc: ~0.95 seconds
> gdc: ~0.79 seconds
> dmd: ~1.77 seconds

For measurements under 1 second, I'm skeptical of the accuracy, because
there could be all kinds of background noise, CPU interrupts and stuff
that could be skewing the numbers.  What about do a best-of-3-runs with
20 million numbers (expected <20 seconds per run) and see how the
numbers look?

Though having said all that, I can say at least that dmd's relatively
poor performance seems in line with my previous observations. :-P The
difference between ldc and gdc is harder to pinpoint; they each have
different optimizers that could work better or worse than the other
depending on the specifics of what the program is doing.


[...]
> I've been mainly a dmd person for various reasons and was under the
> impression that ldc was the clear winner among the three. What is your
> experience? Does gdc compile faster programs in general? Would ldc win
> if I took advantage of e.g. link-time optimizations?
[...]

I'm not sure LDC is the clear winner.  I only prefer LDC because LDC's
architecture makes it easier for cross-compilation (with GCC/GDC you
need to jump through a lot more hoops to get a working cross compiler).
GDC is also tied to the GCC release cycle, and tends to be several
language versions behind LDC.  But both compilers have excellent
optimizers, but they are definitely different so for some things GDC
will beat LDC, for other things LDC will beat GDC. It may depend on the
specific optimization flags you use as well.

But these sorts of statements are just generalizations. The best way to
find out for sure is to disassemble the executable and see for yourself
what the assembly looks like. :-)


T

-- 
Public parking: euphemism for paid parking. -- Flora


More information about the Digitalmars-d-learn mailing list