我参加了一个会议,其中被教导我们不应该使用“使用命名空间 std”,而是使用“std :: cout”来使用 std 命名空间的一些调用,因为这会增加二进制文件的大小
我尝试通过以下实验验证相同的内容。代码及其输出如下:-
[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]$
正如我从我的实验中看到的那样
对二进制文件的大小没有影响
只要
编译时间有差异。
如果我的结论有缺陷,请纠正我
谢谢