简体   繁体   English

.GetProperties()的Big-O

[英]Big-O of .GetProperties()

If there are n properties, then is the Big-O of .GetProperties O(n) or are there are processes involved in the reflection that add complexity? 如果有n个属性,则.GetProperties的Big-O是否为O(n)或反射中是否包含增加复杂性的过程?

Say there is this defined class: 说有这个定义的类:

public class Reflector
{
 public string name { get; set; }
 public int number { get; set; }
 public bool flag { get; set; }
 public List<string> etc { get; set; }
}

And then this call is made: 然后进行此调用:

var reflect = new Reflector();
PropertyInfo[] properties = reflect.GetType().GetProperties();

What is the time complexity, ie Big-O, of .GetProperties() ? .GetProperties()的时间复杂度(即,Big-O .GetProperties()多少? Considering that there are 4 properties, would this only take 4 iterations or is it more complex than that? 考虑到有4个属性,这是否只需要进行4次迭代,或者比这更复杂? Or, is it O(1) with some standard set of complexity to get to the list - which seems it must still be O(n) just to build the properties array? 或者,是将O(1)设置为具有一些标准复杂性的列表吗?似乎只是为了构建属性数组,它仍然必须是O(n)?

More complicated than that. 比这更复杂。 The algorithm has to include the base type chain as well. 该算法还必须包括基本类型链。 In addition, the implementation might cache the result, so the ammortized cost might actually be O(1). 另外,该实现可能会缓存结果,因此摊销后的成本实际上可能是O(1)。

But in practice, reflection is always pretty slow, so you should probably profile your application and make changes until you meet your performance goals. 但是在实践中,反射总是很慢,因此您可能应该分析应用程序并进行更改,直到达到性能目标为止。

Big-O is about asymptotic complexity, in other words O(n) is only relevant for large values of n . Big-O与渐进复杂性有关,换句话说,O(n)仅与n大值有关。
A class will never have enough properties to make this relevant. 一个类将永远不会具有足够的属性来使其与此相关。

For practical purposes, you might as well consider it O(1), but with a very big constant. 出于实际目的,您最好将其视为O(1),但具有很大的常数。

This kind of issue is expressed in nanoseconds, not Big-O notations. 这种问题用纳秒表示,而不是Big-O表示法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM