コード例 #1
0
ファイル: SampleApp.cs プロジェクト: KroneckerX/WCell
        /// <summary>
        /// Test the performance of getting an integer property.
        /// </summary>
        public static void TestGetIntegerPerformance()
        {
            PropertyAccessorTestObject testObject
                = CreateTestObject();

            int test = 0;

            //
            // Generic Property accessor
            //
            Stopwatch watch = new System.Diagnostics.Stopwatch();
            watch.Reset(); watch.Start();

            GenericPropertyAccessor<PropertyAccessorTestObject, int> propertyAccessor =
                new GenericPropertyAccessor<PropertyAccessorTestObject, int>("Int");
            for (int i = 0; i < TEST_ITERATIONS; i++)
            {
                test = propertyAccessor.Get(testObject);
            }

            watch.Stop();
            double genericPropertyAccessorMs = watch.ElapsedMilliseconds;

            //
            // Direct access
            //
            watch.Reset(); watch.Start();

            for (int i = 0; i < TEST_ITERATIONS; i++)
            {
                test = testObject.Int;
            }

            watch.Stop();
            double directAccessMs = watch.ElapsedMilliseconds;

            //
            // Property accessor
            //
            Type type = testObject.GetType();
            watch.Reset(); watch.Start();

            PropertyAccessor propertyAccessor2 = new PropertyAccessor(typeof(PropertyAccessorTestObject), "Int");
            for (int i = 0; i < TEST_ITERATIONS; i++)
            {
                test = (int)propertyAccessor2.Get(testObject);
            }

            watch.Stop();
            double propertyAccessorMs = watch.ElapsedMilliseconds;

            //
            // Print results
            //
            Console.WriteLine(
                TEST_ITERATIONS.ToString() + " property gets on integer..."
                + "\nDirect access ms: \t\t\t\t" + directAccessMs.ToString()
                + "\nGenericPropertyAccessor (Reflection.Emit) ms: \t" + genericPropertyAccessorMs.ToString()
                + "\nPropertyAccessor (Reflection.Emit) ms: \t\t" + propertyAccessorMs.ToString() + "\n\n");
        }
コード例 #2
0
ファイル: SampleApp.cs プロジェクト: KroneckerX/WCell
        /// <summary>
        /// Test the performance of setting a string property.
        /// </summary>
        public static void TestSetStringPerformance()
        {
            const string TEST_VALUE = "Test";

            PropertyAccessorTestObject testObject
                = CreateTestObject();

            //
            // Generic Property accessor
            //
            Stopwatch watch = new System.Diagnostics.Stopwatch();
            watch.Reset(); watch.Start();

            GenericPropertyAccessor<PropertyAccessorTestObject, string> propertyAccessor =
                new GenericPropertyAccessor<PropertyAccessorTestObject, string>("String");
            for (int i = 0; i < TEST_ITERATIONS; i++)
            {
                propertyAccessor.Set(testObject, TEST_VALUE);
            }

            watch.Stop();
            double genericPropertyAccessorMs = watch.ElapsedMilliseconds;

            //
            // Direct access
            //
            watch.Reset(); watch.Start();

            for (int i = 0; i < TEST_ITERATIONS; i++)
            {
                testObject.String = TEST_VALUE;
            }

            watch.Stop();
            double directAccessMs = watch.ElapsedMilliseconds;

            //
            // Property accessor
            //
            Type type = testObject.GetType();
            watch.Reset(); watch.Start();

            PropertyAccessor propertyAccessor2 = new PropertyAccessor(typeof(PropertyAccessorTestObject), "String");
            for (int i = 0; i < TEST_ITERATIONS; i++)
            {
                propertyAccessor2.Set(testObject, TEST_VALUE);
            }

            watch.Stop();
            double propertyAccessorMs = watch.ElapsedMilliseconds;

            //
            // Print results
            //
            Console.WriteLine(
                TEST_ITERATIONS.ToString() + " property sets on string..."
                + "\nDirect access ms: \t\t\t\t" + directAccessMs.ToString()
                + "\nGenericPropertyAccessor (Reflection.Emit) ms: \t" + genericPropertyAccessorMs.ToString()
                + "\nPropertyAccessor (Reflection.Emit) ms: \t\t" + propertyAccessorMs.ToString() + "\n\n");
        }