优化 DataReader 结果:将值保留为对象的结果是什么?
Optimizing DataReader Results: what is the concequence of leaving the values as objects?
var list = new List<object[]>();
var fieldCount = reader.FieldCount;
while(reader.Read()) {
var array = new object[fieldCount];
reader.GetValues(array);
list.Add(array);
}
鉴于上面的简单代码片段。并假设正在检索的值是值类型的混合。
如果事先知道每一列的类型,是否有速度优势
手,并且可以正确地调用有类型的方法来获取(所有)他们的
值?
或者获取键入的值是否会减慢整个读取过程?
将数据保留在对象数组中与将它们存储在类型化实体中有什么取舍?
Is there any speed advantage if you knew the types of each column before hand, and could properly call there typed methods to get (all) their values?
是的。因为您的替代方案正在检查适当的类型,例如:
if (obj is string)
{
//Do string things
}
else if (obj is int)
{
//Do int things
}
当知道类型时,简单地转换:
string t = (string) obj;
Or is getting the typed values slowing the overall read process down?
没有。事实上,你不能比转换为正确的类型更快。
What trade-offs if any are there when leaving the data in object arrays, vs storing them in a typed entity?
将它们存储在对象数组中会占用更多内存,因为 value-types 被装箱为对象。
var list = new List<object[]>();
var fieldCount = reader.FieldCount;
while(reader.Read()) {
var array = new object[fieldCount];
reader.GetValues(array);
list.Add(array);
}
鉴于上面的简单代码片段。并假设正在检索的值是值类型的混合。
如果事先知道每一列的类型,是否有速度优势 手,并且可以正确地调用有类型的方法来获取(所有)他们的 值?
或者获取键入的值是否会减慢整个读取过程?
将数据保留在对象数组中与将它们存储在类型化实体中有什么取舍?
Is there any speed advantage if you knew the types of each column before hand, and could properly call there typed methods to get (all) their values?
是的。因为您的替代方案正在检查适当的类型,例如:
if (obj is string)
{
//Do string things
}
else if (obj is int)
{
//Do int things
}
当知道类型时,简单地转换:
string t = (string) obj;
Or is getting the typed values slowing the overall read process down?
没有。事实上,你不能比转换为正确的类型更快。
What trade-offs if any are there when leaving the data in object arrays, vs storing them in a typed entity?
将它们存储在对象数组中会占用更多内存,因为 value-types 被装箱为对象。