473,385 Members | 1,342 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Namespaces and performance

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
5 2912
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
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

"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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Joshua Beall | last post by:
Hi All, I read in a forum somewhere that namespaces, though once supposed to be a part of PHP5, have been removed. Is this true? Can anyone show me the official statement by Zend that they...
18
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of...
2
by: Jim | last post by:
Hi, When using an XPath query on an XML document that contains namespaces, is there a more efficient way to find a node set based on the local name than //* Is there another syntax that...
2
by: RickN | last post by:
a) Is it best to split up a large application into multiple namespaces and multiple referenced projects or minimize the splits to all required client code in one client dll and all required server...
0
by: kashiDeveloper | last post by:
-------------------------------------------------------------------------------- Hi! Plz anybody can give me idea about using Different My-own-created Namespaces in a single VC++.NET...
11
by: Random | last post by:
I'm confused about the proper use and usefulness of namespaces. I beleive I understand the purpose is so the developer can put classes within namespaces to essentially organize your code. And I...
2
by: tshad | last post by:
If I create multiple .dll files with the same namespace, but different classes. Is this the same thing as creating one file with one Namespace and all the classes in that namespace? If so, is...
7
by: Paul | last post by:
Hello, I'm coming from a PHP background and am working on a large-scale VB.NET project. One of the cornerstones of this project is a reusable form class, to standardize our web forms. My goal...
10
by: Jeff Bigham | last post by:
Hi, Consider the following simplified code in which Foo is used as a 'namespace': var Foo = { doAlert: function() { alert('hello'); }, myEventHandler: function(e) {
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.