Do the amount of namespaces affect performance?

In Visual Studio there is a command to remove unused using statements

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

Is there a performance hit for having unused usings?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The number of namespaces used in code files does not impact the runtime performance of the application. It does have an impact on compile time as the compiler must search those namespaces for additional items such as types and extension methods.

The only runtime impact the number of namespaces I’m aware of are

  • Debugging: The set of used namespaces in a given code file is stored in the PDB and consulted by the debugger during name resolution. Having a lot of namespaces could theoretically impact the performance of the debugger but in practice I haven’t seen this be a problem.
  • Asp.Net: If using the deployment model where pages are compiled on first view by users, the number of namespaces can affect load time the first time a given page is viewed

Method 2

It only affects compilation times when the compiler needs to iterate the namespaces to find the referenced types. (And that aint much anyway.) It wont affect runtime performance at all.

Method 3

No. Namespaces are used to resolve class names at compile time. After compilation, your assembly only contains fully qualified class names like System.Collections.Generic.List<int> myList = new System.Collections.Generic.List<int>(), all the usings are gone.

Method 4

I always thought they were removed away by the compiler.

Method 5

There is no performance hit for unused using statements. They only need evaluation at compile time.

Method 6

I’m sure there is a performance hit somewhere (probably during compilation) but its probably negligible. Either way, I’d recommend running that command – it will remove that potential performance hit and make your code easier to read and maintain. And it’ll remove unused namesapces from intellisense, making it easier to code 🙂


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x