名称空间包含是否会增加exe的大小

时间:2022-10-27 07:29:33

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

我参加了一个会话,其中教导我们不应该使用“using namespace std”,而是使用“std :: cout”来使用std命名空间的一些调用,因为这会增加二进制文件的大小

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

4 个解决方案

#1


3  

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

使用命名空间std不应该影响大多数编译器的二进制大小。由于其他原因,仍应避免:

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.

命名空间std真的很大。其中有数千个标识符,这些标识符都在您的程序范围内。这增加了与您自己的标识符碰撞的可能性或来自其他库的标识符,这可能会导致一些令人讨厌的意外。

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

另请参阅此相关问题:为什么“使用命名空间std”被视为不良做法?

#2


2  

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

二进制文件不一样,因为在一个中你定义了STD,在另一个中你没有定义。我也有不同的尺寸。

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

但是,如果你删除符号,你将获得几乎相同的二进制文件(不同的是一些ELF头参数,如编译时间)。

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.

然后使用#if 1和#if 0进行编译,即使没有条带化符号,也会获得相同大小的二进制文件。

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.

编译时间的差异是正常的。定义宏后,文件会更大,预处理器必须执行更多操作。然而,新的PC是如此强大,我会忽略这个时间的增加。

#3


2  

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.

在任何情况下,即使您确定污染全局命名空间使编译速度稍快,但与可能浪费的追踪名称冲突的时间相比,任何节省的时间都会很小。 std命名空间中有很多名称,其中许多名称可能要在您自己的代码中使用。这就是避免命名空间污染的原因;任何声称它会影响二进制文件大小的人都不能完全理解他们在谈论什么。

#4


2  

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.

我参加了一个会话,其中教导我们不应该使用“using namespace std”,而是使用“std :: cout”来使用std命名空间的一些调用,因为这会增加二进制文件的大小。

Very good advice, nonsense rationale. That rationale is premature optimization, and it is wrong to boot.

很好的建议,废话的理由。这个理由是过早优化,而且启动是错误的。

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

永远不要使用命名空间std;在头文件中。头文件中的该指令使用#include头文件的每个文件中的std命名空间中的项来污染全局命名空间。

Even in a source file, many prefer std::whatever because it makes the code more readable, more understandable, and less prone to error. At the one-time cost of a few characters of typing, that std:: prefix forever communicates intent to the compiler, and more importantly, to the human reader / human maintainer of the code. There's no doubt the code is invoking something from the standard library.

即使在源文件中,许多人更喜欢std :: whatever,因为它使代码更易读,更容易理解,并且更不容易出错。在几次输入字符的一次性成本中,std :: prefix永远将意图传达给编译器,更重要的是,传递给代码的人类读者/人类维护者。毫无疑问,代码正在从标准库中调用某些东西。

The sole justification to employ a using namespace <namespace_name>; directive is sheer laziness on the programmer's part to save a few characters of typing. Those few saved characters of typing come at great expense.

使用using namespace 的唯一理由;指令是程序员的一部分,只是为了节省一些打字字符。那几个保存的打字字符花费很大。

#1


3  

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

使用命名空间std不应该影响大多数编译器的二进制大小。由于其他原因,仍应避免:

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.

命名空间std真的很大。其中有数千个标识符,这些标识符都在您的程序范围内。这增加了与您自己的标识符碰撞的可能性或来自其他库的标识符,这可能会导致一些令人讨厌的意外。

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

另请参阅此相关问题:为什么“使用命名空间std”被视为不良做法?

#2


2  

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

二进制文件不一样,因为在一个中你定义了STD,在另一个中你没有定义。我也有不同的尺寸。

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

但是,如果你删除符号,你将获得几乎相同的二进制文件(不同的是一些ELF头参数,如编译时间)。

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.

然后使用#if 1和#if 0进行编译,即使没有条带化符号,也会获得相同大小的二进制文件。

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.

编译时间的差异是正常的。定义宏后,文件会更大,预处理器必须执行更多操作。然而,新的PC是如此强大,我会忽略这个时间的增加。

#3


2  

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.

在任何情况下,即使您确定污染全局命名空间使编译速度稍快,但与可能浪费的追踪名称冲突的时间相比,任何节省的时间都会很小。 std命名空间中有很多名称,其中许多名称可能要在您自己的代码中使用。这就是避免命名空间污染的原因;任何声称它会影响二进制文件大小的人都不能完全理解他们在谈论什么。

#4


2  

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.

我参加了一个会话,其中教导我们不应该使用“using namespace std”,而是使用“std :: cout”来使用std命名空间的一些调用,因为这会增加二进制文件的大小。

Very good advice, nonsense rationale. That rationale is premature optimization, and it is wrong to boot.

很好的建议,废话的理由。这个理由是过早优化,而且启动是错误的。

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

永远不要使用命名空间std;在头文件中。头文件中的该指令使用#include头文件的每个文件中的std命名空间中的项来污染全局命名空间。

Even in a source file, many prefer std::whatever because it makes the code more readable, more understandable, and less prone to error. At the one-time cost of a few characters of typing, that std:: prefix forever communicates intent to the compiler, and more importantly, to the human reader / human maintainer of the code. There's no doubt the code is invoking something from the standard library.

即使在源文件中,许多人更喜欢std :: whatever,因为它使代码更易读,更容易理解,并且更不容易出错。在几次输入字符的一次性成本中,std :: prefix永远将意图传达给编译器,更重要的是,传递给代码的人类读者/人类维护者。毫无疑问,代码正在从标准库中调用某些东西。

The sole justification to employ a using namespace <namespace_name>; directive is sheer laziness on the programmer's part to save a few characters of typing. Those few saved characters of typing come at great expense.

使用using namespace 的唯一理由;指令是程序员的一部分,只是为了节省一些打字字符。那几个保存的打字字符花费很大。