命名空间的数量是否会影响性能?

时间:2021-04-22 01:04:10

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

在Visual Studio中,有一个命令可以删除未使用的using语句

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?

使用未使用的产品是否会受到性能影响?

6 个解决方案

#1


7  

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.
  • 调试:给定代码文件中使用的命名空间集存储在PDB中,并在名称解析期间由调试器查阅。从理论上讲,拥有大量命名空间可能会影响调试器的性能,但实际上我还没有看到这是一个问题。
  • 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
  • Asp.Net:如果使用部署模型,用户在第一个视图上编译页面,则命名空间的数量会影响第一次查看给定页面时的加载时间

#2


3  

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.

当编译器需要迭代命名空间以查找引用的类型时,它只会影响编译时间。 (无论如何,它还是很多。)它根本不会影响运行时性能。

#3


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.

编号命名空间用于在编译时解析类名。编译之后,程序集只包含完全限定的类名,如System.Collections.Generic.List myList = new System.Collections.Generic.List (),所有的使用都消失了。

#4


1  

I always thought they were removed away by the compiler.

我一直以为它们被编译器删除了。

#5


1  

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

未使用的using语句没有性能损失。他们只需要在编译时进行评估。

#6


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

我确定在某个地方(可能在编译期间)有性能损失,但它可能可以忽略不计。无论哪种方式,我建议运行该命令 - 它将消除潜在的性能损失并使您的代码更易于阅读和维护。并且它将从intellisense中删除未使用的名称,使其更容易编码:)

#1


7  

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.
  • 调试:给定代码文件中使用的命名空间集存储在PDB中,并在名称解析期间由调试器查阅。从理论上讲,拥有大量命名空间可能会影响调试器的性能,但实际上我还没有看到这是一个问题。
  • 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
  • Asp.Net:如果使用部署模型,用户在第一个视图上编译页面,则命名空间的数量会影响第一次查看给定页面时的加载时间

#2


3  

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.

当编译器需要迭代命名空间以查找引用的类型时,它只会影响编译时间。 (无论如何,它还是很多。)它根本不会影响运行时性能。

#3


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.

编号命名空间用于在编译时解析类名。编译之后,程序集只包含完全限定的类名,如System.Collections.Generic.List myList = new System.Collections.Generic.List (),所有的使用都消失了。

#4


1  

I always thought they were removed away by the compiler.

我一直以为它们被编译器删除了。

#5


1  

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

未使用的using语句没有性能损失。他们只需要在编译时进行评估。

#6


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

我确定在某个地方(可能在编译期间)有性能损失,但它可能可以忽略不计。无论哪种方式,我建议运行该命令 - 它将消除潜在的性能损失并使您的代码更易于阅读和维护。并且它将从intellisense中删除未使用的名称,使其更容易编码:)