2
What is the best performance option?
And memory allocation?
Using option 1 or 2?
String text = "ola mundo";
Option 1:
If ( Strings.len(text) > 0 ) {}
Option 2:
If ( text != "") {}
2
What is the best performance option?
And memory allocation?
Using option 1 or 2?
String text = "ola mundo";
Option 1:
If ( Strings.len(text) > 0 ) {}
Option 2:
If ( text != "") {}
6
First, you shouldn’t wear Strings.len()
, this is something used in the Visual Basic compatibilization library and, although possible, it should not be used in new code, especially in C#. Not that it is problematic, just that it keeps using something that is not the default. Use:
var text = "ola mundo";
WriteLine(text.Length > 0);
WriteLine(text != "");
I put in the Github for future reference.
The difference should be small. Both access a very basic data. Some people may think that there will be a count of how many characters the text has, but this information is available and does not need to be calculated.
The second option can be optimized. It may be that the simple comparison of references already indicates that it is the same and does not need to do anything else (I do not think this is the case in this example, but I understand that this example is not quite what you want to know, it was used only to simplify).
It may be that the code comparison hash of string already indicates to be different, of course if he has to calculate the hash can cost expensive, so I have doubts if he would.
Even the worst case when it compares the first character will already identify whether it is equal or not. Since on one side there is only one character, the terminator, no need to check anything else. I am speaking hypothetically, this is not how it works in practice.
In fact we can look at the source code of the function that discovers equality between two strings and see that it is she does the same as option 1 (done in the proper way):
public static bool Equals(String a, String b) {
if ((Object)a==(Object)b) return true;
if ((Object)a==null || (Object)b==null) return false;
if (a.Length != b.Length) return false;
return EqualsHelper(a, b);
}
You can tell there’s two or three branches (comparisons, roughly) before checking if the sizes are equal, this is an extra cost. Even in the size check you have to take the size of the two strings and not just one as in option 1. It is worse to use an out-of-standard comparison.
Note that these issues are implementation details, so what is worth today may not be worth tomorrow and depends a lot on the exact situation.
Although it doesn’t seem clear that option 1 is faster. This may change or may have some optimization that eliminates the code excess of option 2, but in practice we know that this will not occur.
You don’t need to look at the bytecode generated because it is not always indicative of the real performance, except when it is identical, which in this case has not the least chance of being.
If you want to know the exact difference you have to measure, if you just want the fastest is there the information. It can be useful because if your routine is used in a large loop it can give a good performance difference.
Neither of the two options allocate memory, so it makes no difference.
I usually use the String.IsNullOrEmpty()
, is the same ?
@Rovannlinhalis there is different, if you even know if s string is not null, can not use any of the question options, The source of this method is only value == null || value.Length == 0
.
OK, I think it’s valid to mention, usually if you check whether the string is empty, is to work with it later, and a code like that would generate an exception: string valor = null;
 if (valor != "")
 {
 string novoValor= valor.ToUpper();
 }
thank you =]
@Rovannlinhalis exactly. Direct check only if you are sure it is not null, which can be guaranteed even by the compiler in C# 8.
0
I don’t think this is the kind of thing that someone is writing olá mundo
you should be worried.
If you really want to know which one is better or worse, the ideal is for you to look directly at the generated Assembly. Remember to put it in the Release profile, otherwise you won’t have the optimizations.
Another idea is to use Profiling tools.
Browser other questions tagged c# .net string performance memory
You are not signed in. Login or sign up in order to post.
Option 2: If ( text != "") {}
– Daniel Fernandes