Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I read the most interesting benchmark about the speed of string comparisons. It seems there is a HUGE different between the 3 different methods of string comparison. I've also included my summary/notes. I was shocked to find s1.Equals(s2) to be so inefficent in comparison.

 

Source: http://dotnetjunkies.com/WebLog/chris.taylor/archive/2004/05/18/13927.aspx

Google Cached Copy: http://216.239.39.104/search?q=cache:http://dotnetjunkies.com/WebLog/chris.taylor/archive/2004/05/18/13927.aspx

 

using

if (String.Equals("target", "source"))

is 5x faster than using C#'s classic

if (myString == "value")

and it's 30x faster than VB.Net's

if (myString = "value")

 

Please note that

myString.Equals("value")

is extremely inefficient, almost as slow as VB.Net's = comparison, because it does a null check on myString

 

Of course let's interject a reality check here and 99% of the time you won't get any benefit by choosing one method over another. Only in extreme looping situations will it make a difference.

Experience is something you don't get until just after the moment you needed it

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...