Retaining the names inside namespace will make compiler work less stressful!?

For example:

// test.cpp
class vec { /* ... */ };

Take 2 scenarios of main():

// scenario-1
using namespace std;  // comment this line for scenario-2
int main ()
  vec obj;

For scenario-1 where using namespace std;, several type names from namespace std will come into global scope. Thus compiler will have to check against vec if any of the type is colliding with it. If it does then generate error.

In scenario-2 where there is no using namespace, compiler just have to check vec with std, because that's the only symbol in global scope.

I am interested to know that, shouldn't it make the compiler little faster ?

  • 6
    I would focus more on easing maintainer task than compiler task. – mouviciel Oct 19 '12 at 11:00
  • Talk about misguided optimization attempts... Also, if you can't name a dozen ways to speed up the lookup to be constant or near-constant time, and why they are faster, you're not allowed anywhere near performance-sensitive code. – user7043 Oct 19 '12 at 11:12
  • 3
    Can't imagine it makes any significant or measurable difference to the compiler. It will make a difference in the maintainability (and thus correctness of your code). Can imagine the seconds shaved off compile time (compiler per hour unit cost is very small) will ever compensate for the hours trying to resolve even a single bug that was introduced by this (developer per hour cost is very large). – Martin York Oct 19 '12 at 16:04

It depends on how the compiler is designed.

It can make all symbols represented with their own full-qualified names into a binary-search tree, with a another set of "visibility scope" and a coverage table telling what is visible from where (hence the entire name structures is flattened) or it can somehow cope with your name structures or cope with your code block and scopes.

You can easily understand that the compiler performances differ a lot if the compiler developer did an orthogonal choice respect to the compiler user.

However the purpose of the code (apart the obvious doing what is required to do) is to be readable and maintainable, not to be easily compilable. If you plan to optimize compiling, you are focusing on the wrong side of software development, risking to find yourself in 5 years with a code that compiles fast, but you cannot touch anymore since you are anymore able to understand it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.