C# 中小方法调用的成本和优化
我想知道调用短方法的开销是多少,或者代码是否会以任何方式得到优化,以及它是否与吸气剂的成本不同?
我只举一个例子,因为这很难解释。
我有一个网站的声明管理器,它获取特定的声明并返回它们。从一项声明获取另一项声明的过程仅在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在我看来,使用 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. :)
从性能角度来看,getter 和方法之间没有区别。 getter 只是语法糖,在编译期间转换为方法。关于何时使用 getter 和何时使用方法有一些通用准则。 此 msdn 页面建议在以下情况下使用方法而不是属性:
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:
我不会为此使用 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.