6

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

Community
  • 1
  • 1
AnotherDeveloper
  • 2,161
  • 2
  • 23
  • 27
  • There could be differences in compilation time (as in, the `using` could cause more work to be done), but it's very theoretic... – Matthieu M. Sep 04 '12 at 10:50
  • I wouldn't take much notice of the difference in compile time, unless repeated trials show a consistent difference. `g++` will hit at least a few dozen files to compile that program, and timings can vary. – Steve Jessop Sep 04 '12 at 10:50
  • I agree that the compilation time is irrelevant – AnotherDeveloper Sep 04 '12 at 10:56

4 Answers4

3

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?

Community
  • 1
  • 1
Philipp
  • 67,764
  • 9
  • 118
  • 153
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.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
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.

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.

BЈовић
  • 62,405
  • 41
  • 173
  • 273
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.

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.

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.

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.

David Hammen
  • 32,454
  • 9
  • 60
  • 108