12

If I put all classes of a project in the same namespace all classes are available everywhere in the project. But if I use different namespaces not all classes are available everywhere. I get a restriction.

Does using namespaces affect the compile time somehow? Since the compiler has less classes in each namespace and not all namespaces are used all the time he may have a little less trouble finding the right classes.

Will using namespaces affect the applications performance?

TalkingCode
  • 13,407
  • 27
  • 102
  • 147

3 Answers3

15

It won't affect the execution-time performance.

It may affect the compile-time performance, but I doubt that it would be significant at all, and I wouldn't even like to predict which way it will affect it. (Do you have an issue with long compile times? If so, you may want to try it and then measure the difference... otherwise you really won't know the effect. If you don't have a problem, it doesn't really matter.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • If you don't care about hard-realtime (sub-millisecond performance) you should not worry about this. Impact on a human response time is minimal. I would add that coding around the "using namespace" can lead to harder to read code which can have it's own costs. Keep in mind the law of diminishing returns as well ... how fast do you really need to go, and is this core speed ever going to be your bottleneck? If it is a web application ... probably not, so don't worry about it. – Zack Jannsen Apr 21 '13 at 16:14
5

I'm quite sure, that putting classes in namespaces does not effect the compile time significantly.

But beware, that you might lose your logical project-structure, if you put every class into the same namespace.

I (and the Resharper) suggest to use namespaces that correspond with the file location (which corresponds with the project structure).

Alexander Pacha
  • 9,187
  • 3
  • 68
  • 108
2

You should use namespaces according to your logic and ease of human readability and not for performance issues.

Luis Filipe
  • 8,488
  • 7
  • 48
  • 76