Namespaces and performance

G

Guest

Hi,

I'm trying to get some old programs ported to standard C++ and I don't
fancy having to keep typing "std::", so I would like to enter

using namespace std
(I forget if there's a semicolon, but I can look that up)

If I do so, does it affect the performance of my code? (I'm only
worried about at runtime, not compile-time)

Again, can the run-time performance of my code be affected if I use
the using line instead of std:: in a normal C++ program using other
namespaces?

Thanks (and sorry I haven't yet got replies together to everyone who's
answered my questions so far),

James McLaughlin.
 
B

Bob Hairgrove

Hi,

I'm trying to get some old programs ported to standard C++ and I don't
fancy having to keep typing "std::", so I would like to enter

using namespace std
(I forget if there's a semicolon, but I can look that up)

There is a semicolon ;-)
If I do so, does it affect the performance of my code? (I'm only
worried about at runtime, not compile-time)

No. But you SHOULD be worried about compile-time "performance" ...
especially if you put "using namespace std;" in a header file, which
is a big NO-NO. There are issues here which go far beyond run-time
performance.
Again, can the run-time performance of my code be affected if I use
the using line instead of std:: in a normal C++ program using other
namespaces?

No. All namespaces are resolved at compile time, i.e. if you have:

namespace foo {
class A {};
}

and:

namespace bar {
class A {};
}

then foo::A and bar::A are distinct identifiers which are mangled by
the compiler such that the linker will be able to identify them as
distinct entities later. This has no run-time impact, AFAIK.
 
G

Gianni Mariani

Hi,

I'm trying to get some old programs ported to standard C++ and I don't
fancy having to keep typing "std::", so I would like to enter

using namespace std
(I forget if there's a semicolon, but I can look that up)

If I do so, does it affect the performance of my code? (I'm only
worried about at runtime, not compile-time)

This is probably a platform specific question, but as far as I know no
run-time performance issues exist if you use "using namespace" on any of
the compiler implementations that I have used.
 
J

Joe C

Bob Hairgrove said:
There is a semicolon ;-)


No. But you SHOULD be worried about compile-time "performance" ...
especially if you put "using namespace std;" in a header file, which
is a big NO-NO. There are issues here which go far beyond run-time
performance.

Bob, I understand the issue with pulling namespace std into a header file.
However is there any pragmatic reason not to use the std namespace in
implementation (eg *.cpp) files?
 
J

Jonathan Turkanis

Gianni Mariani said:
This is probably a platform specific question, but as far as I know no
run-time performance issues exist if you use "using namespace" on any of
the compiler implementations that I have used.

Strictly speaking, it depends on the implementation. But only an
insane implementation would result in a runtime penalty.

Jonathan
 
B

Bob Hairgrove

Bob, I understand the issue with pulling namespace std into a header file.
However is there any pragmatic reason not to use the std namespace in
implementation (eg *.cpp) files?

If you put it AFTER all the #include lines, it is probably OK.
However, it is best to limit te scope to using directives to
individual function bodies, or better still to use "using std::cout;"
type declarations within a function (I can never remember which is
which ... directives and declarations ...)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,490
Latest member
Finplus

Latest Threads

Top