-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Use .Length == 0
instead of == string.Empty
#3008
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Also smells of |
@miloush, Yes I checked that. But it's better to double check in case I missed something. |
....DotNet.Wpf/src/PresentationFramework/System/Windows/Input/Command/CommandValueSerializer.cs
Show resolved
Hide resolved
src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XmlnsDictionary.cs
Outdated
Show resolved
Hide resolved
@miloush I checked all too. Thank you @Youssef1313 and it looks good for me. |
…ows/Markup/XmlnsDictionary.cs
Short question: |
I think both of these are very fast. Sorry, I can't answer your question directly. |
@lindexi const int Iterations = 10000000;
void Main()
{
BenchNotEqual();
BenchGreaterThan();
}
void BenchNotEqual() {
var list = new List<bool>();
var watch = new Stopwatch();
watch.Start();
for(var i = 0; i < Iterations; i++) {
list.Add(RunNotEqual(i));
}
watch.Stop();
Console.WriteLine($"Not equal took: {watch.ElapsedMilliseconds}ms");
}
void BenchGreaterThan()
{
var list = new List<bool>();
var watch = new Stopwatch();
watch.Start();
for (var i = 0; i < Iterations; i++)
{
list.Add(RunGreaterThan(i));
}
watch.Stop();
Console.WriteLine($"Greater than took: {watch.ElapsedMilliseconds}ms");
}
[MethodImplAttribute(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
bool RunNotEqual(int number) {
return number != 0;
}
[MethodImplAttribute(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
bool RunGreaterThan(int number)
{
return number > 0;
} I ran this script about 10 times and greater than was faster.
Feel free to try, maybe the results are not the same everywhere. |
@deeprobin Sorry, I do not think your benchmark is correct. |
The easiest way to check which is fastest is to check the generated native code from the JIT. In my example I'll use x86 for simplicity. Both checks generate almost the same native code with the exception of the instruction for the comparison ( I would also recommend using BenchmarkDotNet to do micro-benchmark like this, instead of using LinqPad because the actual code that you want to compare gets watered down by other code that are way slower which ends up corrupting your results. Use sharplab.io to check native code: |
src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/PackageDigitalSignatureManager.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/PackageDigitalSignatureManager.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/NameScope.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: Igor Velikorossov <[email protected]>
@RussKie can we get this merged? Thanks! |
@RussKie Do you know who can give a second review and merge? |
@Youssef1313 This is under review by @dotnet/wpf-developers |
@Youssef1313 , thank you for your contribution. |
Checking
.Length == 0
is a bit faster than== string.Empty
.For more information, see CA1820.