Skip to content

Use .Length == 0 instead of == string.Empty - #3008

Merged
dipeshmsft merged 15 commits into
dotnet:mainfrom
Youssef1313:patch-2
Dec 14, 2022
Merged

dipeshmsft merged 15 commits into
dotnet:mainfrom
Youssef1313:patch-2

Conversation

@Youssef1313

Copy link
Copy Markdown
Member

Checking .Length == 0 is a bit faster than == string.Empty.
For more information, see CA1820.

@Youssef1313
Youssef1313 requested a review from a team as a code owner May 17, 2020 11:17
@ghost ghost added the PR metadata: Label to tag PRs, to facilitate with triage label May 17, 2020
@ghost
ghost requested review from SamBent, fabiant3 and ryalanms May 17, 2020 11:17
@Youssef1313
Youssef1313 marked this pull request as draft May 17, 2020 11:35
@Youssef1313
Youssef1313 marked this pull request as ready for review May 17, 2020 11:54
@miloush

miloush commented May 17, 2020

Copy link
Copy Markdown
Contributor

Also smells of NullReferenceException. Did you check all the places you updated guarantee the string is non-null?

@Youssef1313

Copy link
Copy Markdown
Member Author

@miloush, Yes I checked that. But it's better to double check in case I missed something.

@lindexi

lindexi commented May 18, 2020

Copy link
Copy Markdown
Member

@miloush I checked all too.

Thank you @Youssef1313 and it looks good for me.

@ryalanms ryalanms added the Performance Performance related issue label Jan 7, 2021
Base automatically changed from master to main March 17, 2021 17:38
@deeprobin

Copy link
Copy Markdown
Contributor

Short question:
Is value.Length != 0 faster than value.Length > 0?

@lindexi

lindexi commented Jul 7, 2021

Copy link
Copy Markdown
Member

Short question:
Is value.Length != 0 faster than value.Length > 0?

I think both of these are very fast. Sorry, I can't answer your question directly.

@deeprobin

Copy link
Copy Markdown
Contributor

@lindexi
I wrote a little benchmark in LinqPad:

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.

Not equal took: 156ms
Greater than took: 132ms

Feel free to try, maybe the results are not the same everywhere.

@lindexi

lindexi commented Jul 7, 2021

Copy link
Copy Markdown
Member

@deeprobin Sorry, I do not think your benchmark is correct.

@ThomasGoulet73

Copy link
Copy Markdown
Contributor

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 (setne for != and setg for >). So it's just the difference of both of these instruction. I think it is fair to say that both instruction are pretty close to each other because if they weren't, someone would probably already have modified the JIT to use the fastest. Also, given the speed of modern processor, I wouldn't bother comparing two of these similar instruction because the difference is likely to small to be even measurable (Probably not 156 ms vs 132 ms).

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:
https://sharplab.io/#v2:C4LghgzgtgPgAgJgIwFgBQcDMACR2DC2A3utmbjgEYD21ANtgHLXACiAjgK5h0AUcSAAzYAbj04BTAJSlyJNOUW4A7KPESAdABkJAOwDmwABbYAhAF5sggNyyyAXzvYnWbDXrYA4gCcJYYBLeACpGYLr8Qmp0kjIKck6KcKpi0Zo6BsbYAHxWtnEO6PZAA==

Co-authored-by: Igor Velikorossov <[email protected]>
@Youssef1313

Copy link
Copy Markdown
Member Author

@RussKie can we get this merged? Thanks!

@RussKie

RussKie commented Jan 7, 2022 via email

Copy link
Copy Markdown
Contributor

@Youssef1313

Copy link
Copy Markdown
Member Author

@RussKie Do you know who can give a second review and merge?

@singhashish-wpf

Copy link
Copy Markdown
Contributor

@Youssef1313 This is under review by @dotnet/wpf-developers

@dipeshmsft

Copy link
Copy Markdown
Member

@Youssef1313 , thank you for your contribution.

@Youssef1313
Youssef1313 deleted the patch-2 branch December 14, 2022 12:53
@ghost ghost locked as resolved and limited conversation to collaborators Jan 27, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Performance Performance related issue PR metadata: Label to tag PRs, to facilitate with triage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants