17

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?

O.O
  • 11,077
  • 18
  • 94
  • 182
  • Even if there's no performance hit, that doesn't make it a good idea. There's little I hate more than seeing the use of a class and having to figure out WHICH particular one it is because someone has `using`-d every namespace under the sun. – Billy ONeal Dec 07 '10 at 16:45
  • I know what you mean. However, every task has to be prioritized and planned :). – O.O Dec 07 '10 at 16:48

6 Answers6

10

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
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • When it comes to ASP.NET aren't pages optionally compiled when the user enters the root folder? Hence, a performance hit? Or am I way off here? Ref: http://msdn.microsoft.com/en-us/library/ms178466.aspx – O.O Dec 07 '10 at 16:46
  • @subt13 it does affect the startup time of ASP.Net pages which are compiled on first use by the user. I'll add that scenario. – JaredPar Dec 07 '10 at 16:49
  • Excellent. Great answer/comments. – O.O Dec 07 '10 at 16:51
5

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.

codymanix
  • 28,510
  • 21
  • 92
  • 151
4

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.

sisve
  • 19,501
  • 3
  • 53
  • 95
1

I always thought they were removed away by the compiler.

  • Indeed, they are only used by the compiler. At runtime, namespaces are only a part of the fully qualified class names. – codymanix Dec 07 '10 at 16:33
1

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

driis
  • 161,458
  • 45
  • 265
  • 341
0

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 :)

Colin O'Dell
  • 8,386
  • 8
  • 38
  • 75