c# - Does string.IsNullOrEmpty have performance issues? -


is there difference between 2 ways of checking string?

if(!string.isnullorempty(mystring)) {   //do } 

and

if(mystring != null && mystring != "") {   //do } 

so far though no, saying so. , he's saying second 1 better first 1 requires method call.

i'm little confused.

like every 1 else answered, assumed there no performance difference, ran benchmark sure. results not expected...

here's code benchmark:

using system; using system.diagnostics;  static class program {     static void main()     {         int count = 1;          // first run jit warm-up         isnullorempty(null, count);         testequalsempty(null, count);         testlengthzero(null, count);          count = 1000000000;          console.writeline("case 1: s == \"test\"");         runtests("test", count);          console.writeline("case 2: s == null");         runtests(null, count);          console.writeline("case 3: s == \"\"");         runtests("", count);     }      static void runtests(string s, int count)     {         var ts = isnullorempty(s, count);         console.writeline("\tisnullorempty:         {0}", ts);          ts = testlengthzero(s, count);         console.writeline("\ttest if s.length == 0: {0}", ts);          ts = testequalsempty(s, count);         console.writeline("\ttest if s == \"\":       {0}", ts);     }      static timespan isnullorempty(string s, int count)     {         var sw = stopwatch.startnew();         (int = 0; < count; i++)         {             if (string.isnullorempty(s))             {             }         }         sw.stop();         return sw.elapsed;     }      static timespan testlengthzero(string s, int count)     {         var sw = stopwatch.startnew();         (int = 0; < count; i++)         {             if (s == null || s.length == 0)             {             }         }         sw.stop();         return sw.elapsed;     }      static timespan testequalsempty(string s, int count)     {         var sw = stopwatch.startnew();         (int = 0; < count; i++)         {             if (s == null || s == "")             {             }         }         sw.stop();         return sw.elapsed;     } } 

and here results:

 case 1: s == "test"         isnullorempty:         00:00:00.6000748         test if s.length == 0: 00:00:00.5566793         test if s == "":       00:00:02.2284007 case 2: s == null         isnullorempty:         00:00:00.5556170         test if s.length == 0: 00:00:00.5569102         test if s == "":       00:00:00.5554338 case 3: s == ""         isnullorempty:         00:00:00.5568344         test if s.length == 0: 00:00:00.5556285         test if s == "":       00:00:03.0626445 

(compiled optimisations enabled; these results 32-bit clr, results similar 64-bit clr)

so can see, if string not null, calling isnullorempty faster comparing null , "", , fast comparing null , testing if length 0. if string null, 3 methods have identical performance.

so, doing this:

if(mystring != null && mystring != "") 

is going slower, not faster, this:

if(string.isnullorempty(mystring)) 

you this:

if(mystring != null && mystring.length > 0) 

but performance gain small compared calling isnullorempty, , harm readability.

note benchmark run 1,000,000,000 (1 billion) iterations; necessary notice actual difference. in real-world scenario, difference small notice; micro-optimisation.


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -