开发者

Cost of small method calls in C# and optimization

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 开发者_JS百科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


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜