views:

205

answers:

6

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?

+3  A: 

Recall that strings are immutable. System.String.Length never changes.

John Saunders
This does not answer the question if the method takes O(1) time - the method could still be O(n) for example.
Daniel Brückner
@Daniel: this fact, plus common sense, plus the fact that there are "n" people reviewing the code, for n>1000, and assuming that 1% of them have the sense God gave a college Freshman, pretty much _does_ prove that the property runs in constant time. No mathematics required.
John Saunders
+15  A: 

That's bad advice - String.Length is indeed O(1). It's not like strlen 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...

Jon Skeet
@Jon I'd really would look at the context first though, say the call is being unnecessarily in a loop with millions of iterations ... overall, in those cases is just good practice not to call a property if its the same for the whole loop.
eglasius
@Freddy: I disagree - if it's the simplest code and it's evaluating a very simple property which is going to be inlined, I'd rather do that than introduce a temporary local variable. In some cases (array lengths) the JIT does *better* using the property than with a temporary local...
Jon Skeet
@Freddy Rios, I don't know, but would not be surprised that the compiler knows strings are immutable and optimizes the code accordingly. It would, I think, be a rather easy optimization for a compiler.
tster
+3  A: 

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.

Matti Virkkunen
A: 

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.

Eric Hauser
+1  A: 

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.

this. __curious_geek
+2  A: 

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.

Simon Chadwick