Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

does namespace inclusion increases size of exe

I attended a session in which it was taught that we should not use "using namespace std", instead do "std::cout" for using some call of the std namespace as this shall increase size of the binary

I tried verifying the same with the following experiment. The code & its output is as follows:-

    [Fooo@EXP]$ cat namespacestd.cpp
    #include<iostream>

    #ifdef STD
            using namespace std;
    #endif

    int main()
    {
    #ifndef STD
            std::cout<<"\n ==> Workign \n";
    #else
            cout<<"\n ==> Workign \n";
    #endif

    return 0;

    }


    [Fooo@EXP]$ time g++ -c namespacestd.cpp -DSTD

    real    0m0.246s
    user    0m0.215s
    sys     0m0.030s
    [Fooo@EXP]$ size namespacestd.o
       text    data     bss     dec     hex filename
        310       8       1     319     13f namespacestd.o  
    [Fooo@EXP]$ time g++ -c namespacestd.cpp

    real    0m0.258s
    user    0m0.224s
    sys     0m0.034s
    [Fooo@EXP]$ size namespacestd.o
       text    data     bss     dec     hex filename
        310       8       1     319     13f namespacestd.o

    [Fooo@EXP]$ time g++ -o namespacestd namespacestd.cpp -DSTD

    real    0m0.293s
    user    0m0.251s
    sys     0m0.042s
    [Fooo@EXP]$ size namespacestd
       text    data     bss     dec     hex filename
       1980     580     288    2848     b20 namespacestd
    [Fooo@EXP]$ time g++ -o namespacestd namespacestd.cpp

    real    0m0.274s
    user    0m0.239s
    sys     0m0.035s
    [Fooo@EXP]$ size namespacestd
       text    data     bss     dec     hex filename
       1980     580     288    2848     b20 namespacestd
    [Fooo@EXP]$

As I see from my experiment that

there is no effect on the size of binary

only

there is a difference in the compilation time.

Kindly correct me if my conclusions are flawed

Thanks

like image 551
AnotherDeveloper Avatar asked Sep 04 '12 10:09

AnotherDeveloper


3 Answers

using namespace std shouldn't affect binary size with most compilers. It should still be avoided for another reason:

The namespace std is really big. There are literally thousands of identifier in there which are all in the scope of your program. This increases the likeliness of collisions with your own identifiers or identifiers from other libraries which can cause some nasty surprises.

See also this related question: Why is "using namespace std" considered bad practice?

like image 71
Philipp Avatar answered Nov 07 '22 10:11

Philipp


The binaries are not the same, because in one you have STD defined, and in another you don't. I also get different sizes.

However, if you strip symbols, you are going to get almost identical binaries (what is different are some ELF header parameters, like compilation time).

If you change you example to this :

#include<iostream>

    using namespace std;


    int main()
    {
    #if 0
            std::cout<<"\n ==> Workign \n";
    #else
            cout<<"\n ==> Workign \n";
    #endif

    return 0;

    }

and then compile with #if 1 and #if 0, you are going to get binaries of the same size, even without striping symbols.

Difference in compilation time is normal. With the macro defined, the file is larger, and the preprocessor has to do more. However, new PCs are so powerful that I would simply ignore this time increase.

like image 2
BЈовић Avatar answered Nov 07 '22 10:11

BЈовић


there is no effect on the size of binary

There shouldn't be any difference to the executable code and data, since in both cases you're doing the same thing to the same object, and the lookup process to find that object from its name happens during compilation. However, it's possible that in some circumstances they might generate different amounts of metadata, such as debugging information.

there is a difference in the compilation time

Any change to the source could potentially change compilation time, but you haven't presented enough data to determine whether the difference is statistically significant. You'd need to repeat the experiment several times for each configuration, calculate the mean and variance of the two samples, and apply a significance test to the difference of means.

In any event, even if you do determine that polluting the global namespace makes compilation fractionally faster, any time saved will be tiny compared to the time potentially wasted tracking down name collisions. There are a lot of names in the std namespace, many of which you might want to use in your own code. That is the reason for avoiding namespace pollution; anyone claiming that it will affect the size of the binary does not fully understand what they are talking about.

like image 2
Mike Seymour Avatar answered Nov 07 '22 09:11

Mike Seymour