Thread overview
Size of Compiled Program
Jan 04, 2016
Basile B.
Jan 04, 2016
Marc Schütz
Jan 04, 2016
Adam D. Ruppe
Jan 04, 2016
Adam D. Ruppe
Jan 05, 2016
Jacob Carlborg
January 04, 2016
When I was writing a small speed test - D versus Ruby,
calculating the first n prime numbers, I realized, that for small n
Ruby may be faster, than compiling and executing with D.
But for n = 1,000,000 D outperforms Ruby by app. 10x.

Looking at the size of my prime executable, it was around 800 kB with DMD
and even with optimization and "gdc -Os" > 1 MB.
Why is such a short program resulting in a so big binary?
-----------------------------------------------------------------------------
import std.stdio;
import std.conv;
int[] prime; // Dynamic Array of Prime

// Ist n durch eine der zahlen in prime teilbar?
bool teilbar(int n){
        for(auto i=0; prime[i]*prime[i]<=n ; i++){
                if (n%prime[i]==0) return true;
        }
        return false;

}

void main(string[] args)   // first parameter number of primes to calculate
{
        auto anzahl = 10; // default
        prime ~= 2;       // first element of the array
        if(args.length==2){
                anzahl = to!int(args[1]);}
        auto pruefe=1;
        while (prime.length <= anzahl){
                pruefe+=2;
                if(!teilbar(pruefe)){
                        write(" ",pruefe);
                        prime ~= pruefe; // append the array
                }
        }

        write("\n das wars...:",prime.length);
}



January 04, 2016
On Monday, 4 January 2016 at 13:49:03 UTC, Martin Tschierschke wrote:
> When I was writing a small speed test - D versus Ruby,
> calculating the first n prime numbers, I realized, that for small n
> Ruby may be faster, than compiling and executing with D.
> But for n = 1,000,000 D outperforms Ruby by app. 10x.
>
> Looking at the size of my prime executable, it was around 800 kB with DMD
> and even with optimization and "gdc -Os" > 1 MB.
> Why is such a short program resulting in a so big binary?

You have the runtime and phobos compiled with your program. But also:
- if debug info are generated this increases the size.
- if bounds checking is turned off there is some code generated for each array operation
- if contracts are not off there is a lot of assertion that will be generated

see also some clues here:
http://forum.dlang.org/post/mailman.20.1441974998.22025.digitalmars-d-learn@puremagic.com


January 04, 2016
On Monday, 4 January 2016 at 13:49:03 UTC, Martin Tschierschke wrote:
> When I was writing a small speed test - D versus Ruby,
> calculating the first n prime numbers, I realized, that for small n
> Ruby may be faster, than compiling and executing with D.
> But for n = 1,000,000 D outperforms Ruby by app. 10x.
>
> Looking at the size of my prime executable, it was around 800 kB with DMD
> and even with optimization and "gdc -Os" > 1 MB.
> Why is such a short program resulting in a so big binary?

That's probably basic these compilers statically link the runtime (and standard?) libraries by default. Compiling your program with `ldc2 -O3`, I get a binary of 28K, and stripping gets it down to 17K.
January 04, 2016
On Monday, 4 January 2016 at 13:49:03 UTC, Martin Tschierschke wrote:
> When I was writing a small speed test - D versus Ruby

The smallest possible ruby program has about ~5 MB of dependencies, outside the operating system (the ruby runtime itself).

The D program has none. It carries its runtime with it, which makes the executable a bit larger to compensate but helps compatibility with other computers because the user doesn't have to hunt down obscure D runtime packages.
January 04, 2016
On Monday, 4 January 2016 at 14:16:54 UTC, Marc Schütz wrote:
> On Monday, 4 January 2016 at 13:49:03 UTC, Martin Tschierschke wrote:
>> When I was writing a small speed test - D versus Ruby,
>> calculating the first n prime numbers, I realized, that for small n
>> Ruby may be faster, than compiling and executing with D.
>> But for n = 1,000,000 D outperforms Ruby by app. 10x.
>>
>> Looking at the size of my prime executable, it was around 800 kB with DMD
>> and even with optimization and "gdc -Os" > 1 MB.
>> Why is such a short program resulting in a so big binary?
>
> That's probably basic these compilers statically link the runtime (and standard?) libraries by default. Compiling your program with `ldc2 -O3`, I get a binary of 28K, and stripping gets it down to 17K.

Ok, I will try ldc2, too.
January 04, 2016
On Monday, 4 January 2016 at 14:51:59 UTC, Adam D. Ruppe wrote:
> On Monday, 4 January 2016 at 13:49:03 UTC, Martin Tschierschke wrote:
>> When I was writing a small speed test - D versus Ruby
>
> The smallest possible ruby program has about ~5 MB of dependencies, outside the operating system (the ruby runtime itself).
>
> The D program has none. It carries its runtime with it, which makes the executable a bit larger to compensate but helps compatibility with other computers because the user doesn't have to hunt down obscure D runtime packages.

Oh, thats interesting. When I tried to run the compiled "prime" on my notebook,
with the "same" Ubuntu release, I got an error, may be its 32 not 64 Bit?
Any hint?



January 04, 2016
On Monday, 4 January 2016 at 14:01:18 UTC, Basile B. wrote:
> On Monday, 4 January 2016 at 13:49:03 UTC, Martin Tschierschke wrote:
[...]
> - if debug info are generated this increases the size.
> - if bounds checking is turned off there is some code generated for each array operation
> - if contracts are not off there is a lot of assertion that will be generated
>
> see also some clues here:
> http://forum.dlang.org/post/mailman.20.1441974998.22025.digitalmars-d-learn@puremagic.com

Ah, thank you for that Link!
January 04, 2016
On Monday, 4 January 2016 at 16:56:15 UTC, Martin Tschierschke
> with the "same" Ubuntu release, I got an error, may be its 32 not 64 Bit?
> Any hint?

Yeah, probably.
January 05, 2016
On 2016-01-04 17:56, Martin Tschierschke wrote:

> Oh, thats interesting. When I tried to run the compiled "prime" on my
> notebook,
> with the "same" Ubuntu release, I got an error, may be its 32 not 64 Bit?
> Any hint?

You can run the "file" command to see which architecture an executable is built for.

-- 
/Jacob Carlborg