5

The sample code like this:

string hi = "HelloWorld";
int length = hi.Length;
Console.WriteLine(length);
Console.WriteLine(length);
...

string hi = "HelloWorld";
int length = hi.Length;
Console.WriteLine(hi.Length);
Console.WriteLine(hi.Length);
...

If the Length property of string will be accessed more than once, is the first snippet code better than the second? Why?

When we access the length of string in the way of hi.Length, the CLR will count the number of character in hi and return it or just return a 10 cause the Length property has been assigned a value of 10 at the time hi is initialized or something else?

How about in Java?

Marty
  • 3,485
  • 8
  • 38
  • 69
Wenbo
  • 111
  • 6
  • 1
    Possible duplicate of [What does the .NET String.Length property return? Surrogate neutral length or complete character length](http://stackoverflow.com/questions/5656472/what-does-the-net-string-length-property-return-surrogate-neutral-length-or-co) – Michael Jan 22 '16 at 09:32
  • 1
    @VMA - no, the poster is asking if the overhead of calling `hi.length` makes it worth storing the value for reuse – Hugh Jones Jan 22 '16 at 10:00
  • 1
    Are you going to learn the "best" way to write 10 trillion different pieces of code and then blindly stick to using them despite what they may do to readability? First rule of optimization is: don't. Write *simple*, *clear*, understandable code, whilst at the same time setting performance *goals*. Then *measure* your performance and evaluate whether you've met the goals. If you haven't, pinpoint *where* the performance issues are and concentrate your efforts there. Micro-optimizing property accesses is highly doubtful. – Damien_The_Unbeliever Jan 22 '16 at 10:27

3 Answers3

11

Length is a property of an array (string is an array of characters internally) which means it's always already available in memory (an array is always fixed length so the Length property won't change). In both cases you should always just use hi.Length instead of declaring another variable. Now you are just saving the length twice in memory (once on the stack for the int and once on the heap for the property).

Edit: As commenters below have pointed out, this method is optimised for ICollection to use the property instead of iterating. If you were to use the linq method .Count(), this would cause your program to iterate over the entire array to count the elements.

Alexander Derck
  • 13,818
  • 5
  • 54
  • 76
3

Your question is about what happens in .NET at a really low level. What really happens when the JIT'ed and optimized code executes might be different from what you expect.

However, a way to answer your question is to look at the IL that is generated:

string hi = "HelloWorld";
int length = hi.Length;
Console.WriteLine(length);
Console.WriteLine(length);

compiles to

ldstr       "HelloWorld"
callvirt    System.String.get_Length
dup         
call        System.Console.WriteLine
call        System.Console.WriteLine

and

string hi = "HelloWorld";
Console.WriteLine(hi.Length);
Console.WriteLine(hi.Length);

(where I have removed the assignment to length because it is not used) compiles to

ldstr       "HelloWorld"
dup         
callvirt    System.String.get_Length
call        System.Console.WriteLine
callvirt    System.String.get_Length
call        System.Console.WriteLine

The first version seems to be slightly more efficient compared to the second because there is only one call to System.String.get_Length and both uses an extra stack location (dup). However, the JIT'er could conceivably inline this call which would then use indirection to read a value from a memory location and then there is hardly any difference.

Notice that the .NET string type stores the length of the string in the string object so there is no need to count the characters in the string. The length is known when the string is created.

Martin Liversage
  • 104,481
  • 22
  • 209
  • 256
0

According to MSDN String.Length is a property that will give you the number of characters in the current String object.

And in your case the second option is comparatively best; since it can avoid an extra int variable. And also String.Length will not create any instance or additional memory space it will return the allocated memory space in the heap for the string.

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88