Does using namespace ...
increase compilation time or can it somehow affect performance? I heard that this affects the compilation time slightly, but still, why?

- 28,141
- 6
- 41
- 93

- 383
- 2
- 10
-
21. Slightly. 2. No, assuming you're talking about runtime performance. – user207421 Feb 09 '20 at 10:56
-
`using namespace std;` in global can lead to unexpected behavior, `using namespace` in global scope in general is a responsibility of programmer – Swift - Friday Pie Feb 09 '20 at 11:00
-
3For more reasons why not to do `using namespace std;` read [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Ted Lyngmo Feb 09 '20 at 11:04
3 Answers
It increases the compilation time ever so slightly because more places are considered during a name lookup, but nothing significant. It doesn't affect the runtime performance at all.

- 28,141
- 6
- 41
- 93
tldr No, it doesn't affect neither compilation time nor run-time
Strictly speaking every change in the source code modifies the compilation time. Using directives, using statements and all forms of aliases don't affect compilation time in any meaningful or note-worthy way.
As for run-time performance the generate code is identical, so absolutely no performance impact whatsoever.
In conclusion don't worry about it, not in the slightest.
I would like to challenge Ayxan statement that the compilation time is is increased because the more places are considered during name lookup. I could argue the opposite: that compilation time is decreased because nested namespaces are looked up just once on the using directive, instead of on every qualified name.
Consider this:
auto x = nsa::nsb::nsc::nsd::class_x{};
auto y = nsa::nsb::nsc::nsd::class_y{};
auto z = nsa::nsb::nsc::nsd::class_z{};
vs
using namespace nsa::nsb::nsc::nsd;
auto x = class_x{};
auto y = class_y{};
auto z = class_z{};
In the first case:
nsa
must be looked up on the global namespacensb
must be looked up on thensa
namespacensc
must be looked up on thensb
namespacensd
must be looked up on thensc
namespaceclass_x
must be looked up on thensd
namespace- repeat all above thee times (for all the qualified items)
In the second case
- just like before, look up
nsa
in the global namespace,nsb
in thensa
etc. Except just once, during the using directive. - add entry in the table lookup for the using directive
- look up
class_x
in the global namespace + the entries brought by the using directives. Do this for all the unqualified names.
I am not saying one is faster than the other. I am saying it's not as simple and black and white. Without a proper benchmark (study really as the codebases wildly differ) we don't know. Anyway, my point is that it doesn't matter anyway as any difference practically doesn't matter.

- 72,283
- 15
- 145
- 224
About performance, it does not.
Namespaces are used to make logical groupings and to avoid potential naming conflicts within C++ applications. Use of namespaces incurs no overhead to application size or performance.
But...
...bringing the entire contents of such a namespace into current scope in this fashion defeats the purpose of namespaces.
https://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1046398336&id=1043284351
About compile time, there is not much documentation I can find about it, but it seems to be consensual that though it slightly affects it, the differences are negligible, bordering the unmeasurable.
C++ performance: using declaratives Vs using declarations Vs using names directly from the library
For C#

- 23,467
- 7
- 28
- 53
-
True, but this talks about application performance only. It doesn't touch on the compilation impact. – bolov Feb 09 '20 at 11:33