我有一个数据表有10000行,我使用foreach循环到数据表来获取每一行,并为每行调用一个函数,需要2秒执行,并为10000行总时间是20000秒,我怎样才能使用多线程来提高性能。这里是我的代码如何在C#中的forech循环中使用多线程#
DataTable AttCollection = getRecord(); // 10000 records
foreach (var ac in AttCollection)
{
AddAttendace(branchID, _ep.Get_V(ac.Key, true, null), ac.Value);
}
public bool AddAttendace(int? branchID, V_HR_EmployeePlacementDetailed emp, DateTime dt)
{
//code...
}
您可以通过创建字典来查找性能,而不是逐行列举查找。您的平均搜索时间为N/2,而字典将为Log2(N)。 – jdweng