Click here to Skip to main content
15,886,788 members
Articles / Programming Languages / C#

Enum Generitized

Rate me:
Please Sign up or sign in to vote.
3.28/5 (10 votes)
12 May 2007CPOL7 min read 35.1K   164   26  
Using Generics to make a different kind of enumeration: easy to comment, and supports many types.
using System;
using System.Data;
using Adapdev.UnitTest;
using Enums;

namespace Performance
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	[TestFixture]
	public class Performance
	{
        [TestFixtureSetUp()]
        public void PerformanceSetUp()
        {
            s1.RealString = "A";
            s2.RealString = "A";
            g1.GenericString = "A";
            g2.GenericString = "A";
        }
				
		/// <summary>
		/// Declares and sets values for a new TestStringEnumClassABC
		/// </summary>
		[Test(Category="Assign value",Description="Create and assign value to TestStringEnumClass.")]
		[Repeat(1000)]
		public void CreateAndAssignValuesToNewABCClass()
		{
			TestStringEnumClassABC abc = new TestStringEnumClassABC();
			abc.Value = TestStringEnumClassABC.A;
			abc = null;
			
			
		}

		/// <summary>
		/// Declares and sets values for a new TestStringEnumABC
		/// </summary>
        [Test(Category = "Assign value", Description = "Create and assign value to TestStringEnum.")]
		[Repeat(1000)]
        public void CreateAndAssignValuesToNewABC()
		{
			TestStringEnumABC abc = new TestStringEnumABC();
			abc.Value = TestStringEnumABC.A;
			abc = null;
			
			
		}


		private TestStringEnumClassABC privateAbcClass = new TestStringEnumClassABC();
		/// <summary>
		/// Declares and sets values for a private TestStringEnumClassABC
		/// </summary>
        [Test(Category = "Assign value", Description = "Assign value to a private class ABC 1000000 times with for.")]
		//[Repeat(500)]
		public void AssignValuesToPrivateStringEnumClassABC()
		{
            for (int i = 0; i < 1000000; i++)
            {
                privateAbcClass.Value = TestStringEnumClassABC.A;
            }
			
		}

		private TestStringEnumABC privateAbc = new TestStringEnumABC();
		/// <summary>
		/// Declares and sets values for a private TestStringEnumABC
		/// </summary>
        [Test(Category = "Assign value", Description = "Assign value to a private String Enum ABC 1000000 times with for.")]
		//[Repeat(500)]
		public void AssignValuesToPrivateStringEnumABC()
		{
            for (int i = 0; i < 1000000; i++)
            {
                privateAbc.Value = TestStringEnumABC.A;
            }
			
		}

        private TestGenericStringEnumClassABC privateGenericAbc = new TestGenericStringEnumClassABC();
        /// <summary>
        /// Declares and sets values for a private TestGenericStringEnumClassABC
        /// </summary>
        [Test(Category = "Assign value", Description = "Assign value to a private generic String Enum ABC 1000000 times with for.")]
        //[Repeat(500)]
        public void AssignValuesToPrivateGenericStringEnumABC()
        {
            for (int i = 0; i < 1000000; i++)
            {
                privateGenericAbc.Value = TestGenericStringEnumClassABC.A;
            }

        }


		/// <summary>
        /// Instantiate TestStringEnumClassABC many times
		/// </summary>
        [Test(Category = "Instantiate", Description = "Instantiate new string class ABC 1000000 times.")]
        //[Repeat(1000)]
        public void InstantiateStringEnumABCClassInner()
		{
			
			for (int i=0 ; i < 1000000; i++)
			{
				TestStringEnumClassABC abc = new TestStringEnumClassABC();
			}
			
		}
		/// <summary>
        /// Instantiate TestStringEnumClassABC many times
		/// </summary>
		[Test(Category="Instantiate",Description="Instantiate new String Enum abc 1000000 times.")]
        //[Repeat(1000)]
        public void InstantiateStringEnumABCInner()
		{
			
			for (int i=0 ; i < 1000000; i++)
			{
				TestStringEnumABC abc = new TestStringEnumABC();
			}
			
		}

        /// <summary>
        /// Instantiates TestGenericEnumClassABC many times 
        /// </summary>
        [Test(Category = "Instantiate", Description = "declare new generic date enum abc 1000000 times.")]
        //[Repeat(1000)]
        public void InstantiateGenericDateABCInner()
        {

            for (int i = 0; i < 1000000; i++)
            {
                TestGenericEnumClassABC abc = new TestGenericEnumClassABC();
            }

        }

        /// <summary>
        /// Instantiates TestGenericStringEnumClassABC many times
        /// </summary>
        [Test(Category = "Instantiate", Description = "declare new generic String enum abc 1000000 times.")]
        //[Repeat(1000)]
        public void InstantiateGenericStringABCInner()
        {

            for (int i = 0; i < 1000000; i++)
            {
                TestGenericStringEnumClassABC abc = new TestGenericStringEnumClassABC();
            }

        }

        [TestSetUp("InstantiateGenericStringClassABCWithClean")]
        public void TestSetUpInstantiateGenericStringClassABCWithClean()
        {
            System.GC.Collect();
        }
        [Test(Category="Instantiate",Description= "Instantiate generic string enum once.")]
        [Repeat(50)]
        public void InstantiateGenericStringClassABCWithClean()
        {
            TestGenericStringEnumClassABC abc = new TestGenericStringEnumClassABC();
        }

        [TestSetUp("InstantiateStringClassABCWithClean")]
        public void TestSetUpInstantiateStringClassABCWithClean()
        {
            System.GC.Collect();
        }
        [Test(Category = "Instantiate", Description = "Instantiate string enum class once.")]
        [Repeat(50)]
        public void InstantiateStringClassABCWithClean()
        {
            TestStringEnumClassABC abc = new TestStringEnumClassABC();
        }


        [TestSetUp("InstantiateStringABCWithClean")]
        public void TestSetUpInstantiateStringABCWithClean()
        {
            System.GC.Collect();
        }
        [Test(Category = "Instantiate", Description = "Instantiate string enum once.")]
        [Repeat(50)]
        public void InstantiateStringABCWithClean()
        {
            TestStringEnumABC abc = new TestStringEnumABC();
        }

        #region test comparison method
        public class TestString
        {
            public string RealString;
        }

        public class TestGeneric<T>
        {
            public T GenericString;
        }

        TestString s1 = new TestString();
        TestString s2 = new TestString();
        TestGeneric<string> g1 = new TestGeneric<string>();
        TestGeneric<string> g2 = new TestGeneric<string>();

        [Test(Category = "Compare", Description = "Compare string with ==")]
        //[Repeat(50)]
        public void CompareStringWithEqualOp()
        {
            long start = DateTime.Now.Ticks;
            for (int i = 0; i < 1000000; i++)
            {
                if (s1.RealString == s2.RealString)
                { }
            }
            Console.WriteLine("CompareStringWithEqualOp: {0:N}", (DateTime.Now.Ticks - start));
        }

        
        [Test(Category = "Compare", Description = "Compare string with CompareTo")]
        //[Repeat(50)]
        public void CompareStringWithCompareTo()
        {
            long start = DateTime.Now.Ticks;
            for (int i = 0; i < 1000000; i++)
            {
                if (s1.RealString.CompareTo(s2.RealString) == 0)
                { }
            }
            Console.WriteLine("CompareStringWithCompareTo: {0:N}",(DateTime.Now.Ticks - start));
        }

        [Test(Category = "Compare", Description = "Compare string with ICompare CompareTo")]
        //[Repeat(50)]
        public void CompareStringWithICompareCompareTo()
        {
            long start = DateTime.Now.Ticks;
            for (int i = 0; i < 1000000; i++)
            {
                if (((IComparable)s1.RealString).CompareTo(s2.RealString) == 0)
                { }
            }
            Console.WriteLine("CompareStringWithICompareCompareTo: {0:N}",(DateTime.Now.Ticks - start));
        }
        [Test(Category = "Compare", Description = "Generic string with CompareTo")]
        //[Repeat(50)]
        public void CompareGenericWithCompareTo()
        {
            long start = DateTime.Now.Ticks;
            for (int i = 0; i < 1000000; i++)
            {
                if (g1.GenericString.CompareTo(g2.GenericString) == 0)
                { }
            }
            Console.WriteLine("CompareGenericWithCompareTo: {0:N}",(DateTime.Now.Ticks - start));
        }

        #endregion test comparison method
    }
}

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)


Written By
Team Leader
United States United States
A biography in this little spot...sure.
I've worked at GTE HawaiianTel. I've worked at Nuclear Plants. I've worked at Abbott Labs. I've consulted to Ameritech Cellular. I've consulted to Zurich North America. I've consulted to International Truck and Engine. Right now, I've consulted to Wachovia Securities to help with various projects. I've been to SHCDirect and now at Cision.

During this time, I've used all kinds of tools of the trade. Keeping it to the more familier tools, I've used VB3 to VB.NET, ASP to ASP/JAVASCRIPT/XML to ASP.NET. Currently, I'm developing with C# and ASP.NET. I built reports in Access, Excel, Crystal Reports, and Business Objects (including the Universes and ETLS). Been a DBA on SQL Server 4.2 to 2000 and a DBA for Oracle. I've built OLTP databases and DataMarts. Heck, I've even done Documentum. I've been lucky to have created software for the single user to thousands of concurrent users.

I consider myself fortunate to have met many different people and worked in many environments. It's through these experiences I've learned the most.

Comments and Discussions