Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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

For example:

// test.cpp
#include</*iostream,vector,string,map*/>
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 ?

share|improve this question
5  
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. –  delnan Oct 19 '12 at 11:12
2  
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). –  Loki Astari Oct 19 '12 at 16:04

1 Answer 1

up vote 3 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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