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

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?
They are declarations that generate no code. Neither is "faster" in terms of execution time -- the question makes no sense.
I see...

Thanks for the answer, jsmith.
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!
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.
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
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!
I use "using namespace std;"... I guess I'm just not scared enough not to.
There is nothing wrong with using namespace std... as long as you don't stick it in a header file.
May I ask Duoas, what did you mean by
as long as you don't stick it in a header file.
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.