C# Equal vs CompareTo

Out of curiosity I decided to run some speed tests on strings using the Equals and CompareTo methods. I wrote some quick code and came up with some interesting results:
















This test was run for 10000000 iterations. The Equals methods trounced the CompareTo method. Under the hood Equals stops when the first character that doesn't match is found. CompareTo continues on to the end of the string all the time. However this was a case insensitive search with no culture information. Adding more variables may cloud the water a bit, but that is going to be reserved for future posts. The code used in the test can be seen below.





using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Threading;

namespace Sandbox
{
    class Program
    {
        static void Main( string[] args )
        {
            const int counter = 10000000;

            int i = 0;

            int count = 0;

            String string1 = "ASq2qw;m;kl1212ed'12,12lml!@)Eki3";
            String string2 = "AS@_)#L:;sladf-030-!+)I@1=0-23i1[pk1=-12i=}";
            String string3 = "ASq2qw;m;kl1212ed'12,12lml!@)Eki3";

            DateTime start;

            TimeSpan span;

            // run some string comparisons
            Console.WriteLine( "Running tests" );
            Console.WriteLine();

            // first test Equals on strings that aren't equal one another
            start = DateTime.Now;

            for( i = 0; i < counter; i++ )
            {
                if( string1.Equals( string2 ) )
                {
                    count++;
                }
            }

            span = DateTime.Now - start;
            Console.WriteLine( "Strings that aren't equal using Equals() : " + span.TotalSeconds );
            
            // second test using CompareTo on strings that aren't equal
            start = DateTime.Now;

            for( i = 0; i < counter; i++ )
            {
                if( string1.CompareTo( string2 ) == 0 )
                {
                    count++;
                }
            }

            span = DateTime.Now - start;
            Console.WriteLine( "Strings that aren't equal using CompareTo() : " + span.TotalSeconds );

            // third test using Equals on strings that are equal
            start = DateTime.Now;

            for( i = 0; i < counter; i++ )
            {
                if( string1.Equals( string3 ) )
                {
                    count++;
                }
            }

            span = DateTime.Now - start;
            Console.WriteLine( "Strings that are equal using Equals() : " + span.TotalSeconds );

            // fourth test using CompareTo on strings that are equal
            start = DateTime.Now;

            for( i = 0; i < counter; i++ )
            {
                if( string1.CompareTo( string3 ) == 0 )
                {
                    count++;
                }
            }

            span = DateTime.Now - start;
            Console.WriteLine( "Strings that are equal using CompareTo() : " + span.TotalSeconds );
            Console.WriteLine();
            Console.WriteLine( "Press any key to continue . . ." );
            Console.ReadKey();

        }
    }
}

Comments

Popular Posts