Which is faster: using namespace std, using std::, or std::

Nov 22, 2008 at 5:14am
Hello everyone.

I wonder about which method is actually faster than the rest.


For small programs (maybe I'm wrong), I thought 'using namespace std;' would be the slowest because it uses the entire standard library. Also, I thought that the fastest may be 'using std::'.

This finally brings me to the question:

Which is faster: 'using namespace std;', 'using std::', or 'std::'?


Would anyone please enlighten me about the efficiency of these commands? Or is it that they are all essentially the same?
Nov 22, 2008 at 5:54am
They are declarations that generate no code. Neither is "faster" in terms of execution time -- the question makes no sense.
Nov 22, 2008 at 6:11am
I see...

Thanks for the answer, jsmith.
Nov 22, 2008 at 11:50am
I think std:: is the fastest than others. std::>using std::>using namespace std!
Because using namespace std search from the whole namespace std. But std::cout has not this problem!Others, using namespace and using std:: may generate variable conflict!
Nov 22, 2008 at 12:35pm
No, it makes no difference.

During execution (after the program is compiled --and when it really matters) it doesn't take any more time than any other variable access would.

During compilation, the object reference is stored in a lookup table the same as all other objects. The only difference is how many entries are listed in the lookup table --and I really doubt you'll be able to notice (let alone clock) any difference in compile time.
Dec 9, 2008 at 1:39pm
Duoas is right, however I would recommend "using namespace std;" as to avoid having to right "std::cout" or whatever for every command. Not only does it take less time to write with "using namespace std;" but it also makes the size of the file you are saving the code to a bit smaller, which some people like to have.
Last edited on Dec 9, 2008 at 1:39pm
Dec 9, 2008 at 1:50pm
Woah. I can save keystrokes and save a few bytes in my source code? And all I have to do is embrace a practice that could produce conflicts to refer to symbols?
I'm convinced!
Dec 9, 2008 at 8:00pm
I use "using namespace std;"... I guess I'm just not scared enough not to.
Dec 9, 2008 at 10:30pm
There is nothing wrong with using namespace std... as long as you don't stick it in a header file.
Dec 10, 2008 at 10:46am
May I ask Duoas, what did you mean by
as long as you don't stick it in a header file.
Dec 10, 2008 at 10:47am
I personally don't like using it as every tutor I've had hates people using it. And myself I prefer using the std::cout anyway just so I know its from std::.

I've read code before as well where someone has done this:
1
2
3
4
void cout()
{
    cout << "Hello World" << endl;
}


Needless to say the above is fail lol
Topic archived. No new replies allowed.