public void CreateInstanceWithDifferentFactories() { const int TEST_ITERATIONS = 1000000; IFactory factory = null; #region new factory = new NewAccountFactory(); // create an instance so that Activators can // cache the type/constructor/whatever factory.CreateInstance(Type.EmptyTypes); GC.Collect(); GC.WaitForPendingFinalizers(); Timer timer = new Timer(); timer.Start(); for (int i = 0; i < TEST_ITERATIONS; i++) { factory.CreateInstance(Type.EmptyTypes); } timer.Stop(); double newFactoryResult = 1000000 * (timer.Duration / (double)TEST_ITERATIONS); #endregion #region activator factory = new ActivatorObjectFactory().CreateFactory(typeof(Account), Type.EmptyTypes); // create an instance so that Activators can // cache the type/constructor/whatever factory.CreateInstance(Type.EmptyTypes); GC.Collect(); GC.WaitForPendingFinalizers(); timer.Start(); for (int i = 0; i < TEST_ITERATIONS; i++) { factory.CreateInstance(Type.EmptyTypes); } timer.Stop(); double activatorFactoryResult = 1000000 * (timer.Duration / (double)TEST_ITERATIONS); #endregion #region Emit factory = new EmitObjectFactory().CreateFactory(typeof(Account), Type.EmptyTypes); // create an instance so that Activators can // cache the type/constructor/whatever factory.CreateInstance(Type.EmptyTypes); GC.Collect(); GC.WaitForPendingFinalizers(); timer.Start(); for (int i = 0; i < TEST_ITERATIONS; i++) { factory.CreateInstance(Type.EmptyTypes); } timer.Stop(); double emitFactoryResult = 1000000 * (timer.Duration / (double)TEST_ITERATIONS); #endregion // Print results Console.WriteLine( "Create " + TEST_ITERATIONS.ToString() + " objects via factory :" + "\nNew : \t\t\t" + newFactoryResult.ToString("F3") + "\nActivator : \t\t" + activatorFactoryResult.ToString("F3")+ " Ratio : " + ((activatorFactoryResult / newFactoryResult)).ToString("F3") + "\nEmit IL : \t\t\t" + emitFactoryResult.ToString("F3") + " Ratio : " + ((emitFactoryResult / newFactoryResult)).ToString("F3")); }
public void TestGetIntegerPerformance() { const int TEST_ITERATIONS = 1000000; Property prop = new Property(); int test = -1; Timer timer = new Timer(); #region Direct access (fastest) GC.Collect(); GC.WaitForPendingFinalizers(); timer.Start(); for (int i = 0; i < TEST_ITERATIONS; i++) { test = -1; test = prop.Int; Assert.AreEqual(int.MinValue, test); } timer.Stop(); double directAccessDuration = 1000000 * (timer.Duration / (double)TEST_ITERATIONS); #endregion #region IL Property accessor GC.Collect(); GC.WaitForPendingFinalizers(); IGetAccessorFactory factory = new GetAccessorFactory(true); IGetAccessor propertyAccessor = factory.CreateGetAccessor(typeof(Property), "Int"); timer.Start(); for (int i = 0; i < TEST_ITERATIONS; i++) { test = -1; test = (int)propertyAccessor.Get(prop); Assert.AreEqual(int.MinValue, test); } timer.Stop(); double propertyAccessorDuration = 1000000 * (timer.Duration / (double)TEST_ITERATIONS); double propertyAccessorRatio = propertyAccessorDuration / directAccessDuration; #endregion #region IBatisNet.Common.Utilities.Object.ReflectionInfo GC.Collect(); GC.WaitForPendingFinalizers(); ReflectionInfo reflectionInfo = ReflectionInfo.GetInstance(prop.GetType()); timer.Start(); for (int i = 0; i < TEST_ITERATIONS; i++) { test = -1; PropertyInfo propertyInfo = (PropertyInfo)reflectionInfo.GetGetter("Int"); test = (int)propertyInfo.GetValue(prop, null); Assert.AreEqual(int.MinValue, test); } timer.Stop(); double reflectionInfoDuration = 1000000 * (timer.Duration / (double)TEST_ITERATIONS); double reflectionInfoRatio = (float)reflectionInfoDuration / directAccessDuration; #endregion #region Reflection GC.Collect(); GC.WaitForPendingFinalizers(); Type type = prop.GetType(); timer.Start(); for (int i = 0; i < TEST_ITERATIONS; i++) { test = -1; PropertyInfo propertyInfo = type.GetProperty("Int", BindingFlags.Public | BindingFlags.SetProperty | BindingFlags.Instance); test = (int)propertyInfo.GetValue(prop, null); Assert.AreEqual(int.MinValue, test); } timer.Stop(); double reflectionDuration = 1000000 * (timer.Duration / (double)TEST_ITERATIONS); double reflectionRatio = reflectionDuration / directAccessDuration; #endregion #region ReflectionInvokeMember (slowest) GC.Collect(); GC.WaitForPendingFinalizers(); timer.Start(); for (int i = 0; i < TEST_ITERATIONS; i++) { test = -1; test = (int)type.InvokeMember("Int", BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.Instance, null, prop, null); Assert.AreEqual(int.MinValue, test); } timer.Stop(); double reflectionInvokeMemberDuration = 1000000 * (timer.Duration / (double)TEST_ITERATIONS); double reflectionInvokeMemberRatio = reflectionInvokeMemberDuration / directAccessDuration; #endregion // Print results Console.WriteLine("{0} property gets on integer...", TEST_ITERATIONS); Console.WriteLine("Direct access: \t\t{0} ", directAccessDuration.ToString("F3")); Console.WriteLine("IMemberAccessor: \t\t{0} Ratio: {1}", propertyAccessorDuration.ToString("F3"), propertyAccessorRatio.ToString("F3")); Console.WriteLine("IBatisNet ReflectionInfo: \t{0} Ratio: {1}", reflectionInfoDuration.ToString("F3"), reflectionInfoRatio.ToString("F3")); Console.WriteLine("ReflectionInvokeMember: \t{0} Ratio: {1}", reflectionInvokeMemberDuration.ToString("F3"), reflectionInvokeMemberRatio.ToString("F3")); Console.WriteLine("Reflection: \t\t\t{0} Ratio: {1}", reflectionDuration.ToString("F3"), reflectionRatio.ToString("F3")); }