Enabling namespace increases exe size

I attended a session that said that we should not use "using namespace std", instead do "std :: cout" to use some call to the std namespace like , this will increase the size of the binary

I tried to verify the same with the following experiment. The code and its output are 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 can be seen from my experiment,

does not affect binary file size

only

There is a difference in compilation time .

Please correct me if my conclusions are wrong.

thanks

+6
source share
4 answers

using the std namespace should not affect the size of binaries with most compilers. It should be avoided for another reason:

The std namespace is really large. There are literally thousands of identifiers that are in the scope of your program. This increases the likelihood of collisions with your own identifiers or identifiers from other libraries, which can cause some unpleasant surprises.

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

+3
source

no effect on binary file size

There should not be any difference with the executable code and data, since in both cases you do the same with the same object, and the search process to find this object from its name occurs during compilation. However, it is possible that in some cases they can generate different amounts of metadata, such as debugging information.

there is a difference in compilation time

Any change in source can potentially change compilation time, but you have not provided enough data to determine if the difference is statistically significant. You will need to repeat the experiment several times for each configuration, calculate the average value and variance of the two samples and apply the significance test to the difference in means.

In any case, even if you determine that pollution of the global namespace makes compilation fractionally faster, any time saved will be negligible compared to the time potentially wasted with tracking name conflicts. There are many names in the std namespace, many of which you can use in your own code. It is for this reason that namespace pollution can be avoided; anyone who claims that this will affect the size of the binary does not fully understand what they are talking about.

+2
source

Binary files do not match, because you defined STD in one, and not in the other. I also get different sizes.

However, if you strip characters, you will get almost identical binaries (which distinguishes some parameters of the ELF header, for example, compilation time).

If you change the example:

 #include<iostream> using namespace std; int main() { #if 0 std::cout<<"\n ==> Workign \n"; #else cout<<"\n ==> Workign \n"; #endif return 0; } 

and then compiled with #if 1 and #if 0 , you will get binary files of the same size, even without alternating characters.

The difference in compilation time is normal. When a macro is defined, the file is larger and the preprocessor needs to do more. However, the new PCs are so powerful that I simply ignore this increase in time.

+2
source

I attended a session that said that we should not use "using namespace std", instead do "std :: cout" to use some call to the std namespace, as this increases the size of the binary.

Very good advice, pointless justification. This rationale is premature optimization, and boot incorrectly.

Never use using namespace std; in the header file. This directive in the header file pollutes the global namespace with elements from the std in every file that # includes your header file.

Even in the source file, many prefer std::whatever , because it makes the code more readable, understandable, and less error prone. With a one-time cost of several input characters, this std:: prefix will forever convey the intention to the compiler and, more importantly, to the person who is the guardian of the person’s code. There is no doubt that the code calls something from the standard library.

The only rationale for using the directive is using namespace <namespace_name>; - This is just laziness on the part of the programmer to save several input characters. Those few stored input characters come at a high expense.

+2
source

Source: https://habr.com/ru/post/924526/


All Articles