String reverse

    class Program

    {

        private const string string2Reverse = "Loremipsumdolorsitamet,consecteturadipiscingelitUtcondimentumbibendumarcuquiscommodoNullamturpisdolor,interdumaccumsanvestibulumet,elementumnecnibhNammattis,augueactristiquecongue,nuncelitadipiscingdui,uthendreritestmaurisetdiamUtpellentesqueauctorerosinposuereVestibulumvitaeeniminelitmolestietristiquepulvinareumiNullanonquamatellusaccumsanluctusUtvulputatetristiquepretiumLoremipsumdolorsitamet,consecteturadipiscingelitDuisvitaepulvinarvelitCrasvitaemattisdiamSedegettellusnibhSuspendissesempertortoratantevestibulumcursus";

        static void Main(string[] args)

        {

 

            Thread.Sleep(1000);

           

            Stopwatch sw = Stopwatch.StartNew();

 

            Trace.WriteLine(StrRev(string2Reverse));

            Trace.WriteLine("Array.Reverse: " + sw.Elapsed);

            Trace.WriteLine("-===================================================================================-");

 

            sw = Stopwatch.StartNew();

            Trace.WriteLine(StrRev1(string2Reverse));

            Trace.WriteLine("Linq: " + sw.Elapsed);

            Trace.WriteLine("-===================================================================================-");

 

            sw = Stopwatch.StartNew();

            Trace.WriteLine(StrRev2(string2Reverse));

            Trace.WriteLine("StringBuilder: " + sw.Elapsed);

            Trace.WriteLine("-===================================================================================-");

 

            sw = Stopwatch.StartNew();

            Trace.WriteLine(StrRev3(string2Reverse));

            Trace.WriteLine("Array: " + sw.Elapsed);

            Trace.WriteLine("-===================================================================================-");

 

            sw = Stopwatch.StartNew();

            Trace.WriteLine(StrRev4(string2Reverse));

            Trace.WriteLine("Unsafe array: " + sw.Elapsed);

            Trace.WriteLine("-===================================================================================-");

 

            sw = Stopwatch.StartNew();

            Trace.WriteLine(StrRev5(string2Reverse));

            Trace.WriteLine("Unsafe array2: " + sw.Elapsed);

            Trace.WriteLine("-===================================================================================-");

 

            Console.ReadKey();

        }

 

        public static string StrRev(string source)

        {

            char []result = source.ToCharArray();

            Array.Reverse(result);

            return new String(result);

        }

 

        public static string StrRev1(string source)

        {                       

            return new String(source.Reverse().ToArray());

        }

 

        public static string StrRev2(string source)

        {

            if (source.Length == 1) return source;

 

            StringBuilder sb = new StringBuilder(source.Length);

            int i = source.Length – 1;

 

            while(i != -1)

            {

                sb.Append(source[i–]);

            }

 

            return sb.ToString();

        }

 

        public static string StrRev3(string source)

        {

            if (source.Length == 1) return source;

 

            char[] resultArray = new char[source.Length];

            int i = 0;

            int j = source.Length – 1;

            char temp = ;

 

            while ( i < j )

            {

                temp = source[i];

                resultArray[i] = source[j];

                resultArray[j] = temp;

                i++;

                j–;

            }

 

            return new String(resultArray);

        }

 

        public static unsafe string StrRev4(string source)

        {

            if (source.Length == 1) return source;

           

            int i = 0;

            int j = source.Length – 1;

 

            fixed (char* sourceArray = source)

            {

                char temp = ;

 

                while ( i < j )

                {

                    temp = sourceArray[i];

                    sourceArray[i] = sourceArray[j];

                    sourceArray[j] = temp;

                    i++;

                    j–;

                }

 

                return new String(sourceArray);

            }           

        }

 

        public static unsafe string StrRev5(string source)

        {

            if (source.Length == 1) return source;           

 

            fixed (char* sourceArray = source)

            {

                char temp = ;

 

                char* i = sourceArray;

                char* j = &sourceArray[source.Length – 1];

 

                while (i < j)

                {

                    temp = *i;

                    *i = *j;

                    *j = temp;

 

                    i++;

                    j–;

                }                

            }

 

            return source;           

        }

 

    }

 

susrucmulubitsevetnatarotrotrepmesessidnepsuShbinsullettegedeSmaidsittameativsarCtilevranivlupeativsiuDtilegnicsipidarutetcesnoc,tematisrolodmuspimeroLmuiterpeuqitsirtetatupluvtUsutculnasmuccasulletamauqnonalluNimueranivlupeuqitsirteitselomtilenimineeativmulubitseVereusopnisorerotcuaeuqsetnelleptUmaidtesiruamtsetirerdnehtu,iudgnicsipidatilecnun,eugnoceuqitsirtcaeugua,sittammaNhbincenmutnemele,temulubitsevnasmuccamudretni,rolodsiprutmalluNodommocsiuqucramudnebibmutnemidnoctUtilegnicsipidarutetcesnoc,tematisrolodmuspimeroL

Array.Reverse: 00:00:00.0176674

-===================================================================================-

susrucmulubitsevetnatarotrotrepmesessidnepsuShbinsullettegedeSmaidsittameativsarCtilevranivlupeativsiuDtilegnicsipidarutetcesnoc,tematisrolodmuspimeroLmuiterpeuqitsirtetatupluvtUsutculnasmuccasulletamauqnonalluNimueranivlupeuqitsirteitselomtilenimineeativmulubitseVereusopnisorerotcuaeuqsetnelleptUmaidtesiruamtsetirerdnehtu,iudgnicsipidatilecnun,eugnoceuqitsirtcaeugua,sittammaNhbincenmutnemele,temulubitsevnasmuccamudretni,rolodsiprutmalluNodommocsiuqucramudnebibmutnemidnoctUtilegnicsipidarutetcesnoc,tematisrolodmuspimeroL

Linq: 00:00:00.0064376

-===================================================================================-

susrucmulubitsevetnatarotrotrepmesessidnepsuShbinsullettegedeSmaidsittameativsarCtilevranivlupeativsiuDtilegnicsipidarutetcesnoc,tematisrolodmuspimeroLmuiterpeuqitsirtetatupluvtUsutculnasmuccasulletamauqnonalluNimueranivlupeuqitsirteitselomtilenimineeativmulubitseVereusopnisorerotcuaeuqsetnelleptUmaidtesiruamtsetirerdnehtu,iudgnicsipidatilecnun,eugnoceuqitsirtcaeugua,sittammaNhbincenmutnemele,temulubitsevnasmuccamudretni,rolodsiprutmalluNodommocsiuqucramudnebibmutnemidnoctUtilegnicsipidarutetcesnoc,tematisrolodmuspimeroL

StringBuilder: 00:00:00.0017977

-===================================================================================-

susrucmulubitsevetnatarotrotrepmesessidnepsuShbinsullettegedeSmaidsittameativsarCtilevranivlupeativsiuDtilegnicsipidarutetcesnoc,tematisrolodmuspimeroLmuiterpeuqitsirtetatupluvtUsutculnasmuccasulletamauqnonalluNimueranivlupeuqitsirteitselomtilenimineeativmulubitseVereusopnisorerotcuaeuqsetnelleptUmaidtesiruamtsetirerdnehtu,iudgnicsipidatilecnun,eugnoceuqitsirtcaeugua,sittammaNhbincenmutnemele,temulubitsevnasmuccamudretni,rolodsiprutmalluNodommocsiuqucramudnebibmutnemidnoctUtilegnicsipidarutetcesnoc,tematisrolodmuspimeroL

Array: 00:00:00.0017920

 

-===================================================================================-

susrucmulubitsevetnatarotrotrepmesessidnepsuShbinsullettegedeSmaidsittameativsarCtilevranivlupeativsiuDtilegnicsipidarutetcesnoc,tematisrolodmuspimeroLmuiterpeuqitsirtetatupluvtUsutculnasmuccasulletamauqnonalluNimueranivlupeuqitsirteitselomtilenimineeativmulubitseVereusopnisorerotcuaeuqsetnelleptUmaidtesiruamtsetirerdnehtu,iudgnicsipidatilecnun,eugnoceuqitsirtcaeugua,sittammaNhbincenmutnemele,temulubitsevnasmuccamudretni,rolodsiprutmalluNodommocsiuqucramudnebibmutnemidnoctUtilegnicsipidarutetcesnoc,tematisrolodmuspimeroL

Unsafe array: 00:00:00.0018522

-===================================================================================-

 

The StrRev4 method changes const string! Wow!

 

Loremipsumdolorsitamet,consecteturadipiscingelitUtcondimentumbibendumarcuquiscommodoNullamturpisdolor,interdumaccumsanvestibulumet,elementumnecnibhNammattis,augueactristiquecongue,nuncelitadipiscingdui,uthendreritestmaurisetdiamUtpellentesqueauctorerosinposuereVestibulumvitaeeniminelitmolestietristiquepulvinareumiNullanonquamatellusaccumsanluctusUtvulputatetristiquepretiumLoremipsumdolorsitamet,consecteturadipiscingelitDuisvitaepulvinarvelitCrasvitaemattisdiamSedegettellusnibhSuspendissesempertortoratantevestibulumcursus

Unsafe array2: 00:00:00.0017094

-===================================================================================-

 

As we can see the Array reverse method is the safe and fast enough way to reverse string.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s