By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,233 Members | 1,846 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,233 IT Pros & Developers. It's quick & easy.

Namespaces and performance

P: n/a
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.
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Fri, 20 Aug 2004 16:59:54 +0100, ze*****************@yahoo.com
wrote:
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.

--
Bob Hairgrove
No**********@Home.com
Jul 22 '05 #2

P: n/a
ze*****************@yahoo.com wrote:
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.
Jul 22 '05 #3

P: n/a

"Bob Hairgrove" <in*****@bigfoot.com> wrote in message
news:ns********************************@4ax.com...
On Fri, 20 Aug 2004 16:59:54 +0100, ze*****************@yahoo.com
wrote:
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.


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?

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.

--
Bob Hairgrove
No**********@Home.com

Jul 22 '05 #4

P: n/a

"Gianni Mariani" <gi****@mariani.ws> wrote in message
news:96********************@speakeasy.net...
ze*****************@yahoo.com wrote:
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.


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

Jonathan
Jul 22 '05 #5

P: n/a
On Fri, 20 Aug 2004 23:09:46 -0400, "Joe C" <jk*****@bellsouth.net>
wrote:
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 ...)

--
Bob Hairgrove
No**********@Home.com
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.