25

I may already know the answer to this question, but I thought it was worth asking anyway. If I have a load of using statements within my code file that aren't being used;

  1. Does that have any sort of detrimental performance impact?
  2. How does the compiler deal with them at compile/run time?

Thanks

Oskar
  • 1,597
  • 4
  • 19
  • 38
  • 4
    You're talking about using **directives** or using **statements** ? – icecrime Nov 12 '10 at 09:47
  • Do you mean runtime performances or compiler performance? The compiler doesn't deal with them at runtime, by definition. – Simone Nov 12 '10 at 09:48
  • Duplicate of http://stackoverflow.com/questions/136278/why-should-you-remove-unnecessary-c-using-directives ? – John Warlow Nov 12 '10 at 09:52
  • using statements, also im talking about runtime performance rather than compile time performance –  Nov 12 '10 at 09:52
  • @Jon - there is no such thing as an "unused using statement"... I assume you mean things like "`using System.Xml;`" - that is a *directive*. The *statement* is "`using(var conn = CreateConnection()) {...}`" – Marc Gravell Nov 12 '10 at 11:14
  • @Marc, my bad...i thought my question way pretty straight forward..but i guess not. –  Nov 12 '10 at 15:25

6 Answers6

23

does that have any sort of detrimental performance impact?

No.

How does the compiler deal with them at compile/run time?

At compile time they do what you'd expect, i.e. act as namespace imports. They don't appear in the compiled binary: any reference to a type uses the fully-qualified name.

The compiler does check the namespace in a using statement even if it's not needed; if you remove a namespace, then all the using statements that refer to it will fail.

To me, redundant using statements are like redundant comments. They make no difference to the compiler, but include too many of them, and you risk confusing your developers.

Tim Robinson
  • 53,480
  • 10
  • 121
  • 138
8

It doesn't affect performance at runtime at all.

It will probably increase compile-time very slightly since:

1) The compiler needs to parse a few more characters
2) It has to look up identifiers among more candidates. But since this probably uses hashtables it shouldn't be expensive either.

I'd guess the compiler slowdown is negligible.

I'd guess it slows Intellisense down a bit more since the list it has to display and filter gets quite a bit longer.

Removing unused usings is more a stylistic than a performance improvement. And you need to be careful about extension methods since they are brought into scope by using statements. For example I don't remove using System.Linq even when it's currently unused.

CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
  • Not exactly related, but could you please explain more why you wouldn't want to remove `using System.Linq` ? I am not exactly following. – Roman Apr 24 '14 at 20:49
  • @Roman Because I like having LINQ methods shown in Intellisense. – CodesInChaos Apr 24 '14 at 22:14
  • Yes, probably the Intellisense infrastructure is the only one that could be talk about performances with unused usings. – Steve Sep 09 '15 at 09:26
2

No performance impact in run time. However, a lot of namespaces can somewhat slow down your code completion and decrease productivity

Dyppl
  • 12,161
  • 9
  • 47
  • 68
1

There is no performance hit at runtime, usings are resolved at compile time and useless ones are ignored.

Andrew Bezzub
  • 15,744
  • 7
  • 51
  • 73
0

It has no impact at all - see response to this question:

How is performance affected by an unused using directive?

Community
  • 1
  • 1
Jazza
  • 1,042
  • 1
  • 9
  • 22
0

It may make the compile time longer, but it will have either no or a negligible effect on run-time performance.

Alexander Rafferty
  • 6,134
  • 4
  • 33
  • 55