Click here to Skip to main content
12,401,864 members (61,168 online)
Click here to Skip to main content

Stats

38.7K views
663 downloads
80 bookmarked
Posted

Optimized Enum.ToString()

, 6 Nov 2011 CPOL
A generic version of Enum, which provides much more faster formatting
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Ideafixxxer.Generics
{
    enum Simple
    {
        Zero,
        One,
        Two,
        Three,
        Four,
        Five
    }

    enum Sparse
    {
        MinusOne = -1,
        One = 1,
        Three = 3,
        Four,
        Five,
        Hundred = 100,
    }

    [Flags]
    enum Flagged
    {
        None = 0,
        One = 1,
        Two = 2,
        Both = 3,
        Four = 4,
        All = 7
    }

    class Program
    {
        static void Main(string[] args)
        {
            Stopwatch sw = new Stopwatch();
            const int count = 1000000;
            long div;

            Console.WriteLine("{0,-50}\t{1,5} {2,6} {3}", "Operation", "Time", "Ratio", "Result");

            #region ToString

            #region Simple.Four.ToString

            sw.Restart();
            for (int i = 0; i < count; i++)
            {
                Simple.Four.ToString();
            }
            sw.Stop();
            Log("Simple.Four.ToString()", div = sw.ElapsedMilliseconds, div, Simple.Four.ToString());

            sw.Restart();
            for (int i = 0; i < count; i++)
            {
                Enum<Simple>.ToString(Simple.Four);
            }
            sw.Stop();
            Log("Enum<Simple>.ToString(Simple.Four)", sw.ElapsedMilliseconds, div, Enum<Simple>.ToString(Simple.Four));

            sw.Restart();
            for (int i = 0; i < count; i++)
            {
                Enum<Simple>.ToString(4);
            }
            sw.Stop();
            Log("Enum<Simple>.ToString(4)", sw.ElapsedMilliseconds, div, Enum<Simple>.ToString(4));

            sw.Restart();
            for (int i = 0; i < count; i++)
            {
                SimpleToStringUsingSwitch(Simple.Four);
            }
            sw.Stop();
            Log("SimpleToStringUsingSwitch(Simple.Four)", sw.ElapsedMilliseconds, div, SimpleToStringUsingSwitch(Simple.Four));

            sw.Restart();
            for (int i = 0; i < count; i++)
            {
                SimpleToStringUsingArray(Simple.Four);
            }
            sw.Stop();
            Log("SimpleToStringUsingArray(Simple.Four)", sw.ElapsedMilliseconds, div, SimpleToStringUsingArray(Simple.Four));

            #endregion

            #region ((Simple) 404).ToString
            Simple simpleValue = (Simple)404;

            sw.Restart();
            for (int i = 0; i < count; i++)
            {
                simpleValue.ToString();
            }
            sw.Stop();
            Log("((Simple) 404).ToString()", div = sw.ElapsedMilliseconds, div, simpleValue.ToString());

            sw.Restart();
            for (int i = 0; i < count; i++)
            {
                Enum<Simple>.ToString(simpleValue);
            }
            sw.Stop();
            Log("Enum<Simple>.ToString((Simple) 404)", sw.ElapsedMilliseconds, div, Enum<Simple>.ToString(simpleValue));

            sw.Restart();
            for (int i = 0; i < count; i++)
            {
                Enum<Simple>.ToString(404);
            }
            sw.Stop();
            Log("Enum<Simple>.ToString(404)", sw.ElapsedMilliseconds, div, Enum<Simple>.ToString(404));

            #endregion

            #region Sparse.Four.ToString

            sw.Restart();
            for (int i = 0; i < count; i++)
            {
                Sparse.Four.ToString();
            }
            sw.Stop();
            Log("Sparse.Four.ToString()", div = sw.ElapsedMilliseconds, div, Sparse.Four.ToString());

            sw.Restart();
            for (int i = 0; i < count; i++)
            {
                Enum<Sparse>.ToString(Sparse.Four);
            }
            sw.Stop();
            Log("Enum<Sparse>.ToString(Sparse.Four)", sw.ElapsedMilliseconds, div, Enum<Sparse>.ToString(Sparse.Four));

            sw.Restart();
            for (int i = 0; i < count; i++)
            {
                Enum<Sparse>.ToString(4);
            }
            sw.Stop();
            Log("Enum<Sparse>.ToString(4)", sw.ElapsedMilliseconds, div, Enum<Sparse>.ToString(4));

            #endregion

            #region ((Sparse) 404).ToString
            Sparse sparseValue = (Sparse)404;

            sw.Restart();
            for (int i = 0; i < count; i++)
            {
                sparseValue.ToString();
            }
            sw.Stop();
            Log("((Sparse) 404).ToString()", div = sw.ElapsedMilliseconds, div, sparseValue.ToString());

            sw.Restart();
            for (int i = 0; i < count; i++)
            {
                Enum<Sparse>.ToString(sparseValue);
            }
            sw.Stop();
            Log("Enum<Sparse>.ToString((Sparse) 404)", sw.ElapsedMilliseconds, div, Enum<Sparse>.ToString(sparseValue));

            sw.Restart();
            for (int i = 0; i < count; i++)
            {
                Enum<Sparse>.ToString(404);
            }
            sw.Stop();
            Log("Enum<Sparse>.ToString(404)", sw.ElapsedMilliseconds, div, Enum<Sparse>.ToString(404));

            #endregion

            #region Flagged.Four.ToString

            sw.Restart();
            for (int i = 0; i < count; i++)
            {
                Flagged.Four.ToString();
            }
            sw.Stop();
            Log("Flagged.Four.ToString()", div = sw.ElapsedMilliseconds, div, Flagged.Four.ToString());

            sw.Restart();
            for (int i = 0; i < count; i++)
            {
                Enum<Flagged>.ToString(Flagged.Four);
            }
            sw.Stop();
            Log("Enum<Flagged>.ToString(Flagged.Four)", sw.ElapsedMilliseconds, div, Enum<Flagged>.ToString(Flagged.Four));

            sw.Restart();
            for (int i = 0; i < count; i++)
            {
                Enum<Flagged>.ToString(4);
            }
            sw.Stop();
            Log("Enum<Flagged>.ToString(4)", sw.ElapsedMilliseconds, div, Enum<Flagged>.ToString(4));

            #endregion

            #region ((Flagged) 404).ToString
            Flagged flaggedValue = (Flagged)404;

            sw.Restart();
            for (int i = 0; i < count; i++)
            {
                flaggedValue.ToString();
            }
            sw.Stop();
            Log("((Flagged) 404).ToString()", div = sw.ElapsedMilliseconds, div, flaggedValue.ToString());

            sw.Restart();
            for (int i = 0; i < count; i++)
            {
                Enum<Flagged>.ToString(flaggedValue);
            }
            sw.Stop();
            Log("Enum<Flagged>.ToString((Flagged) 404)", sw.ElapsedMilliseconds, div, Enum<Flagged>.ToString(flaggedValue));

            sw.Restart();
            for (int i = 0; i < count; i++)
            {
                Enum<Flagged>.ToString(404);
            }
            sw.Stop();
            Log("Enum<Flagged>.ToString(404)", sw.ElapsedMilliseconds, div, Enum<Flagged>.ToString(404));

            #endregion

            #region (Flagged.Two|Flagged.Four).ToString
            flaggedValue = (Flagged)6;

            sw.Restart();
            for (int i = 0; i < count; i++)
            {
                flaggedValue.ToString();
            }
            sw.Stop();
            Log("(Flagged.Two|Flagged.Four).ToString()", div = sw.ElapsedMilliseconds, div, flaggedValue.ToString());

            sw.Restart();
            for (int i = 0; i < count; i++)
            {
                Enum<Flagged>.ToString(flaggedValue);
            }
            sw.Stop();
            Log("Enum<Flagged>.ToString(Flagged.Two|Flagged.Four)", sw.ElapsedMilliseconds, div, Enum<Flagged>.ToString(flaggedValue));

            sw.Restart();
            for (int i = 0; i < count; i++)
            {
                Enum<Flagged>.ToString(6);
            }
            sw.Stop();
            Log("Enum<Flagged>.ToString(6)", sw.ElapsedMilliseconds, div, Enum<Flagged>.ToString(6));

            #endregion

            #region (Flagged.All).ToString

            sw.Restart();
            for (int i = 0; i < count; i++)
            {
                Flagged.All.ToString();
            }
            sw.Stop();
            Log("Flagged.All.ToString()", div = sw.ElapsedMilliseconds, div, Flagged.All.ToString());

            sw.Restart();
            for (int i = 0; i < count; i++)
            {
                Enum<Flagged>.ToString(Flagged.All);
            }
            sw.Stop();
            Log("Enum<Flagged>.ToString(Flagged.All)", sw.ElapsedMilliseconds, div, Enum<Flagged>.ToString(Flagged.All));

            sw.Restart();
            for (int i = 0; i < count; i++)
            {
                Enum<Flagged>.ToString(7);
            }
            sw.Stop();
            Log("Enum<Flagged>.ToString(7)", sw.ElapsedMilliseconds, div, Enum<Flagged>.ToString(7));

            #endregion 
            #endregion

            #region Parse

            #region Simple Four
            sw.Restart();
            for (int i = 0; i < count; i++)
            {
                Enum.Parse(typeof(Simple), "Four");
            }
            sw.Stop();
            Log("Enum.Parse(typeof(Simple), \"Four\")", div = sw.ElapsedMilliseconds, div, Enum.Parse(typeof(Simple), "Four"));

            sw.Restart();
            for (int i = 0; i < count; i++)
            {
                Enum<Simple>.Parse("Four");
            }
            sw.Stop();
            Log("Enum<Simple>.Parse(\"Four\")", sw.ElapsedMilliseconds, div, Enum<Simple>.Parse("Four"));
            
            #endregion

            #region Simple 4
            sw.Restart();
            for (int i = 0; i < count; i++)
            {
                Enum.Parse(typeof(Simple), "4");
            }
            sw.Stop();
            Log("Enum.Parse(typeof(Simple), \"4\")", div = sw.ElapsedMilliseconds, div, Enum.Parse(typeof(Simple), "4"));

            sw.Restart();
            for (int i = 0; i < count; i++)
            {
                Enum<Simple>.Parse("4");
            }
            sw.Stop();
            Log("Enum<Simple>.Parse(\"4\")", sw.ElapsedMilliseconds, div, Enum<Simple>.Parse("4"));
            
            #endregion

            #region Sparse Four
            sw.Restart();
            for (int i = 0; i < count; i++)
            {
                Enum.Parse(typeof(Sparse), "Four");
            }
            sw.Stop();
            Log("Enum.Parse(typeof(Sparse), \"Four\")", div = sw.ElapsedMilliseconds, div, Enum.Parse(typeof(Sparse), "Four"));

            sw.Restart();
            for (int i = 0; i < count; i++)
            {
                Enum<Sparse>.Parse("Four");
            }
            sw.Stop();
            Log("Enum<Sparse>.Parse(\"Four\")", sw.ElapsedMilliseconds, div, Enum<Sparse>.Parse("Four"));
            
            #endregion

            #region Sparse 4
            sw.Restart();
            for (int i = 0; i < count; i++)
            {
                Enum.Parse(typeof(Sparse), "4");
            }
            sw.Stop();
            Log("Enum.Parse(typeof(Sparse), \"4\")", div = sw.ElapsedMilliseconds, div, Enum.Parse(typeof(Sparse), "4"));

            sw.Restart();
            for (int i = 0; i < count; i++)
            {
                Enum<Sparse>.Parse("4");
            }
            sw.Stop();
            Log("Enum<Sparse>.Parse(\"4\")", sw.ElapsedMilliseconds, div, Enum<Sparse>.Parse("4"));
            
            #endregion

            #region Flagged Four
            sw.Restart();
            for (int i = 0; i < count; i++)
            {
                Enum.Parse(typeof(Flagged), "Four");
            }
            sw.Stop();
            Log("Enum.Parse(typeof(Flagged), \"Four\")", div = sw.ElapsedMilliseconds, div, Enum.Parse(typeof(Flagged), "Four"));

            sw.Restart();
            for (int i = 0; i < count; i++)
            {
                Enum<Flagged>.Parse("Four");
            }
            sw.Stop();
            Log("Enum<Flagged>.Parse(\"Four\")", sw.ElapsedMilliseconds, div, Enum<Flagged>.Parse("Four"));
            
            #endregion

            #region Flagged 4
            sw.Restart();
            for (int i = 0; i < count; i++)
            {
                Enum.Parse(typeof(Flagged), "4");
            }
            sw.Stop();
            Log("Enum.Parse(typeof(Flagged), \"4\")", div = sw.ElapsedMilliseconds, div, Enum.Parse(typeof(Flagged), "4"));

            sw.Restart();
            for (int i = 0; i < count; i++)
            {
                Enum<Flagged>.Parse("4");
            }
            sw.Stop();
            Log("Enum<Flagged>.Parse(\"4\")", sw.ElapsedMilliseconds, div, Enum<Flagged>.Parse("4"));
            
            #endregion            

            #region Flagged Two,Four
            sw.Restart();
            for (int i = 0; i < count; i++)
            {
                Enum.Parse(typeof(Flagged), "Two,Four");
            }
            sw.Stop();
            Log("Enum.Parse(typeof(Flagged), \"Two,Four\")", div = sw.ElapsedMilliseconds, div, Enum.Parse(typeof(Flagged), "Two,Four"));

            sw.Restart();
            for (int i = 0; i < count; i++)
            {
                Enum<Flagged>.Parse("Two,Four");
            }
            sw.Stop();
            Log("Enum<Flagged>.Parse(\"Two,Four\")", sw.ElapsedMilliseconds, div, Enum<Flagged>.Parse("Two,Four"));
            
            #endregion              
            #endregion
            Console.ReadKey();
       }

        public static void Log(string operation, long elapsed, long div, object value)
        {
            Console.WriteLine("{0,-50}\t{1,5} {2,6:0.00} {3}", operation, elapsed, (double)div / (double)elapsed, value);
        }

        public static string SimpleToStringUsingSwitch(Simple value)
        {
            switch (value)
            {
                case Simple.One:
                    return "One";
                case Simple.Two:
                    return "Two";
                case Simple.Three:
                    return "Three";
                case Simple.Four:
                    return "Four";
                case Simple.Five:
                    return "Five";
                default:
                    return ((int)value).ToString();
            }
        }

        private static readonly string[] simpleNames = Enum.GetNames(typeof(Simple));

        public static string SimpleToStringUsingArray(Simple value)
        {
            int i = (int)value;
            return i >= 0 && i < simpleNames.Length ? simpleNames[i] : i.ToString();
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

ideafixxxer
Team Leader
Canada Canada
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160721.1 | Last Updated 6 Nov 2011
Article Copyright 2011 by ideafixxxer
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid