C# 中小方法调用的成本和优化

发布于 2024-11-01 04:17:28 字数 1119 浏览 4 评论 0原文

我想知道调用短方法的开销是多少,或者代码是否会以任何方式得到优化,以及它是否与吸气剂的成本不同?

我只举一个例子,因为这很难解释。

我有一个网站的声明管理器,它获取特定的声明并返回它们。从一项声明获取另一项声明的过程仅在 ClaimsType 字符串上有所不同。

public string GetClaimValueByType(string ClaimType)
{
    return (from claim in _claimsIdentity.Claims
              where claim.ClaimType == ClaimType
              select claim.Value).SingleOrDefault();
}

/*Again would this be better or worse if I wanted to be able to choose if 
I want the claim versus the value?

public Claim GetClaimByType(string ClaimType)
{
    return (from claim in _claimsIdentity.Claims
              where claim.ClaimType == ClaimType
              select claim).SingleOrDefault();
}

public string GetClaimValueByType(string ClaimType)
{
    return GetClaimByType(ClaimType).Value;
}
*/    
public string GetEmail()
{
    return GetClaimValueByType(ClaimTypes.Email);
}

/* Or should I use getters?...
public string Email
{
    get
    {
        return return GetClaimValueByType(ClaimTypes.Email);
    }
}
*/

那么使用这些简短的 get 方法是不好的做法吗?由于时间太短,是否应该有很大的调用开销,或者是否会对其进行优化?最后,在这里实际使用吸气剂更有意义吗?...

谢谢

I was wondering what the overhead of calling short methods were or if the code would get optimized either way and if it was different than the cost of getters?

I'll just give an example because it is hard to explain.

I have a ClaimsManager for a website that gets particular claims and returns them. The process for getting one claim from another differs only by a ClaimsType string.

public string GetClaimValueByType(string ClaimType)
{
    return (from claim in _claimsIdentity.Claims
              where claim.ClaimType == ClaimType
              select claim.Value).SingleOrDefault();
}

/*Again would this be better or worse if I wanted to be able to choose if 
I want the claim versus the value?

public Claim GetClaimByType(string ClaimType)
{
    return (from claim in _claimsIdentity.Claims
              where claim.ClaimType == ClaimType
              select claim).SingleOrDefault();
}

public string GetClaimValueByType(string ClaimType)
{
    return GetClaimByType(ClaimType).Value;
}
*/    
public string GetEmail()
{
    return GetClaimValueByType(ClaimTypes.Email);
}

/* Or should I use getters?...
public string Email
{
    get
    {
        return return GetClaimValueByType(ClaimTypes.Email);
    }
}
*/

So is this bad practice to have these short get methods? Should there be a large call overhead because it is so short or will this be optimized? Finally, does it make more sense to actually use getters here?..

Thanks

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

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

发布评论

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

评论(3

讽刺将军 2024-11-08 04:17:28

在我看来,使用 setter 和 getter 可能带来的边际开销都被干净且更易于维护的代码所抵消,这些代码很可能对于任何外行的 .NET 开发人员来说都更容易上手和运行。

但我想这也取决于你的 Claim 对象有多大。 :)

In my opinion, what ever marginal overhead there may be with using setters and getters is outweighed by the clean and more easily maintainable code that would most likely be easier for any .NET developer off the street to pick up and run with.

But I guess it also depends on how huge your Claim object is. :)

吹梦到西洲 2024-11-08 04:17:28

从性能角度来看,getter 和方法之间没有区别。 getter 只是语法糖,在编译期间转换为方法。关于何时使用 getter 和何时使用方法有一些通用准则。 此 msdn 页面建议在以下情况下使用方法而不是属性:

  • 操作是转换,例如 Object.ToString。
  • 该操作的成本足够高,您希望告知用户他们应该考虑缓存结果。
  • 使用 get 访问器获取属性值会产生明显的副作用。
  • 连续两次调用该成员会产生不同的结果。
  • 执行顺序很重要。请注意,类型的属性应该能够以任何顺序设置和检索。
  • 该成员是静态的,但返回一个可以更改的值。
  • 该成员返回一个数组。返回数组的属性可能非常具有误导性。通常需要返回内部数组的副本,以便用户无法更改内部状态。再加上用户可以轻松假设它是索引属性这一事实,会导致代码效率低下。

Performance wise there is no difference between a getter and a method. A getter is just syntactic sugar, and is converted to a method during compilation. There are some general guidelines as to when to use a getter and when to use a method. This msdn page advices to use a method instead of a property when:

  • The operation is a conversion, such as Object.ToString.
  • The operation is expensive enough that you want to communicate to the user that they should consider caching the result.
  • Obtaining a property value using the get accessor would have an observable side effect.
  • Calling the member twice in succession produces different results.
  • The order of execution is important. Note that a type's properties should be able to be set and retrieved in any order.
  • The member is static but returns a value that can be changed.
  • The member returns an array. Properties that return arrays can be very misleading. Usually it is necessary to return a copy of the internal array so that the user cannot change internal state. This, coupled with the fact that a user can easily assume it is an indexed property, leads to inefficient code.
沫雨熙 2024-11-08 04:17:28

我不会为此使用 getter,属性旨在返回一个常量 值。这意味着顺序调用应该返回相同的值。这只是一个概念性的事情。

I wouldn't use a getter for this, properties are intended to return a constant value. This means that that sequenced calls should return the same value. This is just a conceptual thing.

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