一些反编译器的观察结果
在这两种情况下,不同的过载System.Data.DataRowCollection.Add
正在使用方法。
第一种方法使用:
public void Add(DataRow row)
{
this.table.AddRow(row, -1);
}
第二种方法将使用:
public DataRow Add(params object[] values)
{
int record = this.table.NewRecordFromArray(values);
DataRow dataRow = this.table.NewRow(record);
this.table.AddRow(dataRow, -1);
return dataRow;
}
现在,看看这个小野兽:
internal int NewRecordFromArray(object[] value)
{
int count = this.columnCollection.Count;
if (count < value.Length)
{
throw ExceptionBuilder.ValueArrayLength();
}
int num = this.recordManager.NewRecordBase();
int result;
try
{
for (int i = 0; i < value.Length; i++)
{
if (value[i] != null)
{
this.columnCollection[i][num] = value[i];
}
else
{
this.columnCollection[i].Init(num);
}
}
for (int j = value.Length; j < count; j++)
{
this.columnCollection[j].Init(num);
}
result = num;
}
catch (Exception e)
{
if (ADP.IsCatchableOrSecurityExceptionType(e))
{
this.FreeRecord(ref num);
}
throw;
}
return result;
}
特别要注意的是this.columnCollection[i][num] = value[i];
,这将调用:
public DataColumn this[int index]
{
get
{
DataColumn result;
try
{
result = (DataColumn)this._list[index];
}
catch (ArgumentOutOfRangeException)
{
throw ExceptionBuilder.ColumnOutOfRange(index);
}
return result;
}
}
继续前进,我们发现实际上_list
is an ArrayList
:
private readonly ArrayList _list = new ArrayList();
结论
为了总结以上内容,如果您正在使用dtDeptDtl.Rows.Add();
代替dtDeptDtl.Rows.Add(dr2);
,你会得到一个性能下降随着列数的增加,它将呈指数级增长。退化的负责线路是调用NewRecordFromArray
方法,该方法迭代ArrayList
.
注意:如果您向表中添加 8 列并在其中进行一些测试,则可以轻松测试这一点for
循环1000000次。