Quantcast
Channel: Common Language Runtime Internals and Architecture forum
Viewing all articles
Browse latest Browse all 1710

String Comparision

$
0
0

What is the fastest way to check the Equality of strings in .Net?

As per msdn we need to Explicitly use stringcomparision.ordinal to have a increase speed,
but it seems to be wrong in this case.

Without ordinal:

            time = Stopwatch.GetTimestamp();
            if (str.Equals("Hi"))
            {
                Console.WriteLine(true);
            }
            else
            {
                Console.WriteLine(false);
            }
            Console.WriteLine("The time taken = {0}", (Stopwatch.GetTimestamp() - time));

//With ordinal:

           time = Stopwatch.GetTimestamp();
            if (str.Equals("Hi",StringComparison.Ordinal))
            {
                Console.WriteLine(true);
            }
            else
            {
                Console.WriteLine(false);
            }
            Console.WriteLine("The time taken = {0}", (Stopwatch.GetTimestamp() - time));

The output was : 2934 for (i)
                             2646 for (ii)

Can anyone explain?


Viewing all articles
Browse latest Browse all 1710

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>