.NET System.String.Length 属性采用什么时间顺序?
有人建议我避免重复调用 String.Length
,因为每次调用它时都会重新计算。我假设 String.Length
在 O(1) 时间内运行。 String.Length
比这更复杂吗?
I had someone advise me to avoid repeatedly calling String.Length
, because it was recalculated each time I called it. I had assumed that String.Length
ran in O(1) time. Is String.Length
more complex than that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是个糟糕的建议 -
String.Length
确实是 O(1)。它不像 C 中的strlen
。不可否认,它在 文档,但字符串的不变性使得 O(1) 成为一件非常愚蠢的事情不。 (不仅仅是 O(1),而且是一个非常快的恒定时间。)
坦率地说,如果有人给出这样的建议,我会对其他更加怀疑> 他们也可能提供建议...
That's bad advice -
String.Length
is indeed O(1). It's not likestrlen
in C.Admittedly it's not guaranteed in the docs as far as I can tell, but the immutability of strings makes it a pretty silly thing not to make O(1). (And not just O(1), but a very fast constant time too.)
Frankly if someone is giving that sort of advice, I would become a bit more skeptical about other advice they may provide too...
String.Length 为 O(1)。人们告诉您不要在循环中调用它的原因是因为它是属性访问,这与方法调用相同。实际上,一个额外的方法调用很少会产生任何显着的差异。
与往常一样,不要开始检查代码缓存对 String.Length 的所有调用,除非您的探查器表明这是性能问题的根源。
String.Length is O(1). The reason people tell you not to call it in a loop is because it's a property access, which is the same as a method call. In reality one extra method call rarely makes any significant difference.
As always, don't start going through your code caching all calls to String.Length unless your profiler says it's the source of a performance problem.
回想一下,字符串是不可变的。
System.String.Length
永远不会改变。Recall that strings are immutable.
System.String.Length
never changes.不,它不会重新计算。字符串类型是不可变的。
更进一步,根据 .net 框架设计指南,本质上非常静态且非易失性的对象属性将被设计为属性。如果该属性具有易失性,需要在每次调用时重新计算,则应将其作为方法提供。
当您尝试检查某个属性是否需要足够的处理周期来吸引您的注意时,您可以遵循此规则。
No it does not recalculate. String type is immutable.
To take this further, as per the .net framework design guidelines, a property of an object that is very much static and non-volatile in nature would be designed as property. Should the property be of a volatile nature that needs recalculation on every call, it shall be made available as method.
you can take this rule while you attempt to check if a property takes enough processing cycles to attract your attention.
正如其他人所说,String.Length 是一个常量属性。如果您确实关心性能(或有大量迭代),则可以将其值分配给局部整数变量一次,然后多次读取(在循环中等)。这将使优化器有更好的机会将该值分配给 CPU 寄存器。访问属性是比堆栈变量或寄存器昂贵得多的操作。
As others have said String.Length is a constant property. If you really care about performance (or have significant iterations), you could assign its value to a local integer variable once, and read that many times (in a loop, etc.). This would give the optimizer a better chance of allocating this value to a CPU register. Accessing a property is a much more expensive operation than a stack variable or a register.
根据内部注释,String.Length 属性是一条不运行 for 循环的指令。因此,这是一个 O(1) 的操作。
According to the internal comments, the String.Length property is a single instruction that does not run a for loop. Therefore, it is an O(1) operation.