Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is more efficient, "data.Length==0" or "data==string.Empty"?

I want to check whether or not a variable string data contain empty string.

Which is more efficient, data.Length==0 or data==string.Empty?

I forgot to say, the data has been checked and it is guaranteed to be not null.

like image 329
xport Avatar asked Nov 28 '22 01:11

xport


1 Answers

Test results for 100 million iterations:

Equality operator ==:   796 ms 
string.Equals:          811 ms 
string.IsNullOrEmpty:   312 ms 
Length:                 140 ms  [fastest]
Instance Equals:       1077 ms 

source

like image 78
Peter Avatar answered Dec 18 '22 07:12

Peter