【发布时间】:2020-02-22 10:48:00
【问题描述】:
- 对不起,非计算机科学术语(自学)
- 抱歉,术语不正确(任何愿意编辑的人,请做,(自学))
我尽可能多地搜索,但我对所搜索内容的术语知识有限,但我找不到并回答我想要的问题。
我惊讶地发现通过Reflection 向对象填充数据比使用object Initialization 更快。
我为此目的制作了一个测试控制台应用程序。 首先是测试类,
class TestClass
{
public string PropertyOne { get; set; }
public string PropertyTwo { get; set; }
public string PropertyThree { get; set; }
public string PropertyFour { get; set; }
public string PropertyFive { get; set; }
public string PropertySix { get; set; }
public string PropertySeven { get; set; }
public string PropertyEight { get; set; }
public string PropertyNine { get; set; }
public string PropertyTen { get; set; }
}
}
然后是获取数据的方法, 首先是反射,
public static void ReflectionTest()
{
for (int i = 0; i < 10000000; i++)
{
TestClass testClass = new TestClass();
Type type = testClass.GetType();
PropertyInfo[] properties = type.GetProperties();
foreach (var propertyInfo in properties)
{
switch (propertyInfo.Name)
{
case nameof(testClass.PropertyOne):
propertyInfo.SetValue(testClass, "PropertyOne" + i);
break;
case nameof(testClass.PropertyTwo):
propertyInfo.SetValue(testClass, "PropertyTwo" + i);
break;
case nameof(testClass.PropertyThree):
propertyInfo.SetValue(testClass, "PropertyThree" + i);
break;
case nameof(testClass.PropertyFour):
propertyInfo.SetValue(testClass, "PropertyFour" + i);
break;
case nameof(testClass.PropertyFive):
propertyInfo.SetValue(testClass, "PropertyFive)" + i);
break;
case nameof(testClass.PropertySix):
propertyInfo.SetValue(testClass, "PropertySix" + i);
break;
case nameof(testClass.PropertySeven):
propertyInfo.SetValue(testClass, "PropertySeven" + i);
break;
case nameof(testClass.PropertyEight):
propertyInfo.SetValue(testClass, "PropertyEight" + i);
break;
case nameof(testClass.PropertyNine):
propertyInfo.SetValue(testClass, "PropertyNine" + i);
break;
case nameof(testClass.PropertyTen):
propertyInfo.SetValue(testClass, "PropertyTen" + i);
break;
}
}
TestClasses.Add(testClass);
}
}
接下来是初始化对象,
public static void InitializationTest()
{
for (int i = 0; i < 10000000; i++)
{
TestClass testClass = new TestClass
{
PropertyOne = "PropertyOne" + i,
PropertyTwo = "PropertyTwo" + i,
PropertyThree = "PropertyThree" + i,
PropertyFour = "PropertyFour" + i,
PropertyFive = "PropertyFive)" + i,
PropertySix = "PropertySix" + i,
PropertySeven = "PropertySeven" + i,
PropertyEight = "PropertyEight" + i,
PropertyNine = "PropertyNine" + i,
PropertyTen = "PropertyTen" + i
};
TestClasses.Add(testClass);
}
}
还有测试代码
static List<TestClass> TestClasses { get; set; } = new List<TestClass>();
static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
sw.Start();
ReflectionTest();
Console.WriteLine($"Reflection Test time: {sw.Elapsed}");
sw.Reset();
sw.Start();
InitializationTest();
Console.WriteLine($"Initialization Test time: {sw.Elapsed}");
sw.Stop();
}
通过对象初始化,使用此代码反射的速度提高了 20%。 这是什么原因?
编辑:在代码中添加TestClasses.Clear(); 表明“对象初始化”几乎快两倍。感谢您的回复和cmets。
【问题讨论】:
-
您最好在运行
InitializationTest()之前将TestClasses集合重置为空,否则您将包括调整非常大数组大小所花费的时间。当我注释掉TestClasses.Add(testClass);(这真的不应该成为测试的一部分!)时,我的结果给出了大约 30 秒的反射和 8 秒的初始化(对于 RELEASE 构建),使初始化速度提高了 3 倍! -
性能测试是一门艺术,而不是一个大循环计数器的小事。
-
@MatthewWatson,有道理。我重做了代码,你是对的。
标签: c# reflection