关于c#优化器的问题

发布于 2024-07-17 07:25:17 字数 263 浏览 5 评论 0原文

如果我写:

SomeType simpleName = classWithLongName.otherLongName;

然后使用“simpleName”而不是“classWithLongName.otherLongName”,这会以任何方式改变程序(例如性能方面)吗?

编译器用这个做什么? 它是否在我使用“simpleName”的任何地方复制+粘贴“classWithLongName.otherLongName”。

If I write:

SomeType simpleName = classWithLongName.otherLongName;

And then use "simpleName" instead of "classWithLongName.otherLongName", will this change the program in any way (for instance performance wise)?

What does the compiler do with this? Does it copy+paste "classWithLongName.otherLongName", everywhere I use "simpleName".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

待天淡蓝洁白时 2024-07-24 07:25:17

不,C# 编译器不会将对“simpleName”的调用转换为复制并粘贴“classWithLongName.otherLongName”。 差异可能是深刻的,也可能只是语义上的,但您所做的是将 classWithLongName.otherLongName 中的值分配给 simpleName。 该类型是值类型还是引用类型将准确确定操作该值时会发生什么以及将发生什么,但这样做时并没有创建函数指针或委托。

它是否会对性能产生影响确实不是这里可以回答的问题,只能说它不会产生负面影响。 我们不能说它是否会产生积极的影响,因为这取决于您调用 classWithLongName.otherLongName 时实际发生的情况。 如果这是一个昂贵的操作,那么这可能会使其速度更快,但缺点是,如果您将其值缓存在 中,则后续调用 classWithLongName.otherLongName 时的任何值差异都不会反映出来>简单名称。

No, the C# compiler doesn't translate a call to "simpleName" to be the same as copying and pasting "classWithLongName.otherLongName". The difference could be profound or simply semantic, but what you're doing is assigning the value from classWithLongName.otherLongName to simpleName. Whether the type is a value type or a reference type will determine exactly what happens and what will happen if you manipulate that value, but you're not creating a function pointer or delegate in doing that.

Whether it will have an effect on performance really isn't something that can be answered here, other than to say that it won't have a NEGATIVE effect. We can't say if it will have a positive effect, since that would depend on what actually happens when you call classWithLongName.otherLongName. If that's an expensive operation, then this could make it faster, but the downside would be that any differences in value upon subsequent calls to classWithLongName.otherLongName wouldn't be reflected if you cached its value in simpleName.

假情假意假温柔 2024-07-24 07:25:17

这取决于“otherLongName”实际在做什么。 如果它是一个属性,那么区别在于多次执行该属性还是只执行一次。 这可能会也可能不会显着改变程序的行为,具体取决于它正在做什么。

It depends what "otherLongName" is actually doing. If it's a property, then the difference is between executing the property several times or only executing it once. That may or may not change the behaviour of the program in a significant way, depending on what it's doing.

浅笑依然 2024-07-24 07:25:17

仅当您始终键入“classWithLongName.otherLongName”时,如果编译器知道该值在过程中不会更改,则仅允许编译器缓存该值并重新使用它本身。 然而,这种情况很少发生。

因此,如果“classWithLongName.otherLongName”确实执行了一些计算,通常可以按照您的建议将其手动缓存在局部变量中,从而获得更好的性能。 但是,请记住,您正在使用缓存值,原始值或属性的更改不会反映在缓存值上。

然而,名称的长度只是元数据,对运行时性能没有任何影响,因为名称在编译期间已解析为内部句柄。

The compiler is only allowed to cache the value and re-use it itself when you always type "classWithLongName.otherLongName" if it knows that the value will not change in the course. However, this is seldom the case.

Therefore, if "classWithLongName.otherLongName" does perform some computation, you'll usually get better performance by caching it manually in a local variable as you suggested. However, keep in mind that you are working with a cached value and that changes in the original value or property will not be reflected on your cached value.

The length of the name however is just metadata and has no influence whatsoever on runtime performance, since the name is already resolved to an internal handle during compilation.

她说她爱他 2024-07-24 07:25:17

这是关于实例还是类的问题?

例如

namespace MyCompany.MyApp.LongNamespaceName
{
    public class MyClassWithALongName {

        public SomeType AnInstanceProperty {get;set;}

        public static SomeType AStaticProperty {get { ... }}
    }
}

现在:

//this gets the static property
SomeType simpleName = MyClassWithALongName.AStaticProperty;

或者:

MyClassWithALongName anInstanceWithALongName = new MyClassWithALongName();

//this gets the instance property
SomeType simpleName = anInstanceWithALongName.AnInstanceProperty;

这些将以不同的方式表现。

不过,这里还有另一种情况,您可以为类的实际名称创建别名:

using simpleName = MyCompany.MyApp.LongNamespaceName.MyClassWithALongName;

...
simpleName anInstance = new simpleName (); 

Is this a question about instances or classes?

For instance

namespace MyCompany.MyApp.LongNamespaceName
{
    public class MyClassWithALongName {

        public SomeType AnInstanceProperty {get;set;}

        public static SomeType AStaticProperty {get { ... }}
    }
}

Now:

//this gets the static property
SomeType simpleName = MyClassWithALongName.AStaticProperty;

Alternatively:

MyClassWithALongName anInstanceWithALongName = new MyClassWithALongName();

//this gets the instance property
SomeType simpleName = anInstanceWithALongName.AnInstanceProperty;

These will behave in different ways.

There's another case here though, you can create an alias for the actual name of the class:

using simpleName = MyCompany.MyApp.LongNamespaceName.MyClassWithALongName;

...
simpleName anInstance = new simpleName (); 
寂寞花火° 2024-07-24 07:25:17
  • 如果 classWithLongName.otherLongName 是属性,则对 simpleName 的更改不会更改 classWithLongName.otherLongName。

    如果 classWithLongName.otherLongName 是属性,则

  • 如果 classWithLongName.otherLongName 是值类型的公共数据成员(字段),则对 simpleName 的更改不会更改 classWithLongName.otherLongName。

  • 如果 classWithLongName.otherLongName 是引用类型的公共数据成员(字段),则对 simpleName 的更改将更改 classWithLongName.otherLongName。

  • If classWithLongName.otherLongName is a property, than changes to simpleName will NOT change classWithLongName.otherLongName.

  • If classWithLongName.otherLongName is a public data member (a field) of a value type, than changes to simpleName will NOT change classWithLongName.otherLongName.

  • If classWithLongName.otherLongName is a public data member (a field) of a reference type, than changes to simpleName WILL change classWithLongName.otherLongName.

三月梨花 2024-07-24 07:25:17

假设您的类型是对象(引用)类型,则 simpleName 最终将包含对 classWithLongName.otherLongName 返回的对象的引用。 如果您要对该对象上的属性进行大量调用,那么您可能会获得性能改进,特别是当 otherLongName 是属性而不是字段时。

Assuming your type is an object (reference) type then simpleName will end up containing a reference to the object returned by classWithLongName.otherLongName. If you are then going to make lots of calls to properties on that object then you may get a performance improvement, especially if otherLongName is a property as opposed to a field.

浅忆流年 2024-07-24 07:25:17

你总是可以把它变成一个函数。

SomeType simpleName() { return classWithLongName.otherLongName; }

You can always make it a function.

SomeType simpleName() { return classWithLongName.otherLongName; }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文