O.O
O.O

Reputation: 11287

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?

Upvotes: 17

Views: 3911

Answers (6)

JaredPar
JaredPar

Reputation: 754705

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

Upvotes: 10

Colin O'Dell
Colin O'Dell

Reputation: 8647

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

Upvotes: 0

driis
driis

Reputation: 164291

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

Upvotes: 1

codymanix
codymanix

Reputation: 29468

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.

Upvotes: 5

sisve
sisve

Reputation: 19781

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.

Upvotes: 4

Robin Williams
Robin Williams

Reputation: 143

I always thought they were removed away by the compiler.

Upvotes: 1

Related Questions