方法一
IEnumerable<DataGridViewRow> dgvRows = this.dtCoordDataView.Rows.OfType<DataGridViewRow>();
System.Threading.Tasks.Parallel.ForEach(dgvRows, dataRow =>
{
string pointName = dataRow.Cells[this.PesNumColCount - 1].Value.ToString();
if (pointName.Length >= 2)
{
string pesName = pointName.Substring(pesNumStartSel, 2);
if (PesCodeNotNull(pesName))
{
DataTable dt = accDb.Table("select * from " + pesName + hz);
// DataTable dt = Wendao.WdSystem.Databases.Access.AccessDbEd.Table("select * from " + pesName + hz, oleConn);
bool notNull = false;
foreach (DataRow dtRow in dt.Rows)
{
string tPtName = dtRow[pointFieldName].ToString();
if (tPtName.Trim().ToUpper() == pointName.Trim().ToUpper())
{
notNull = true;
break;
}
}
if (!notNull)
{
pointNames.Add(pointName);
}
}
}
});方法二
foreach (DataGridViewRow dataRow1 in this.dtCoordDataView.Rows)
{
string pointName = dataRow1.Cells[this.PesNumColCount - 1].Value.ToString();
if (pointName.Length >= 2)
{
string pesName = pointName.Substring(pesNumStartSel, 2);
if (PesCodeNotNull(pesName))
{
DataTable dt = accDb.Table("select * from " + pesName + hz);
// DataTable dt = Wendao.WdSystem.Databases.Access.AccessDbEd.Table("select * from " + pesName + hz, oleConn);
bool notNull = false;
foreach (DataRow dtRow in dt.Rows)
{
string tPtName = dtRow[pointFieldName].ToString();
if (tPtName.Trim().ToUpper() == pointName.Trim().ToUpper())
{
notNull = true;
break;
}
}
if (!notNull)
{
pointNames.Add(pointName);
}
}
}方法一用了PLINQ的Parallel.ForEach方法,方法二用了一般的foreach
按理说方法一应该比方法二快,但是我运行下来方法一却比方法二慢了2秒多。请问是什么原因造成的啊?
IEnumerable<DataGridViewRow> dgvRows = this.dtCoordDataView.Rows.OfType<DataGridViewRow>();
System.Threading.Tasks.Parallel.ForEach(dgvRows, dataRow =>
{
string pointName = dataRow.Cells[this.PesNumColCount - 1].Value.ToString();
if (pointName.Length >= 2)
{
string pesName = pointName.Substring(pesNumStartSel, 2);
if (PesCodeNotNull(pesName))
{
DataTable dt = accDb.Table("select * from " + pesName + hz);
// DataTable dt = Wendao.WdSystem.Databases.Access.AccessDbEd.Table("select * from " + pesName + hz, oleConn);
bool notNull = false;
foreach (DataRow dtRow in dt.Rows)
{
string tPtName = dtRow[pointFieldName].ToString();
if (tPtName.Trim().ToUpper() == pointName.Trim().ToUpper())
{
notNull = true;
break;
}
}
if (!notNull)
{
pointNames.Add(pointName);
}
}
}
});方法二
foreach (DataGridViewRow dataRow1 in this.dtCoordDataView.Rows)
{
string pointName = dataRow1.Cells[this.PesNumColCount - 1].Value.ToString();
if (pointName.Length >= 2)
{
string pesName = pointName.Substring(pesNumStartSel, 2);
if (PesCodeNotNull(pesName))
{
DataTable dt = accDb.Table("select * from " + pesName + hz);
// DataTable dt = Wendao.WdSystem.Databases.Access.AccessDbEd.Table("select * from " + pesName + hz, oleConn);
bool notNull = false;
foreach (DataRow dtRow in dt.Rows)
{
string tPtName = dtRow[pointFieldName].ToString();
if (tPtName.Trim().ToUpper() == pointName.Trim().ToUpper())
{
notNull = true;
break;
}
}
if (!notNull)
{
pointNames.Add(pointName);
}
}
}方法一用了PLINQ的Parallel.ForEach方法,方法二用了一般的foreach
按理说方法一应该比方法二快,但是我运行下来方法一却比方法二慢了2秒多。请问是什么原因造成的啊?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货