我正在比较从二进制文件生成的两个数据列表。我对它为什么运行缓慢有一个好主意,当有大量记录时,它会执行不必要的冗余工作。
例如,如果a1 = a1,则条件为true。由于2a!= 1a那么为什么还要检查呢?我需要从再次检查中消除1a。如果没有,它将在检查第40万条记录时检查第一个记录。我曾考虑过将第二个for循环作为foreach,但在迭代嵌套循环时无法删除1a
可以在“ for循环”中使用的项目数量可能有所不同。我认为使用'i'的单个for循环不起作用,因为匹配可以在任何地方。我正在从二进制文件读取
这是我当前的代码。程序已经运行了一个多小时,并且仍在继续。出于可读性原因,我删除了很多迭代代码。
for (int i = 0; i < origItemList.Count; i++)
{
int modFoundIndex = 0;
Boolean foundIt = false;
for (int g = 0; g < modItemList.Count; g++)
{
if ((origItemList[i].X == modItemList[g].X)
&& (origItemList[i].Y == modItemList[g].Y)
&& (origItemList[i].Z == modItemList[g].Z)
&& (origItemList[i].M == modItemList[g].M))
{
foundIt = true;
modFoundIndex = g;
break;
}
else
{
foundIt = false;
}
}
if (foundIt)
{
/*
* This is run assumming it finds an x,y,z,m
coordinate. It thenchecks the database file.
*
*/
//grab the rows where the coordinates match
DataRow origRow = origDbfFile.dataset.Tables[0].Rows[i];
DataRow modRow = modDbfFile.dataset.Tables[0].Rows[modFoundIndex];
//number matched indicates how many columns were matched
int numberMatched = 0;
//get the number of columns to match in order to detect all changes
int numOfColumnsToMatch = origDbfFile.datatable.Columns.Count;
List<String> mismatchedColumns = new List<String>();
//check each column name for a change
foreach (String columnName in columnNames)
{
//this grabs whatever value is in that field
String origRowValue = "" + origRow.Field<Object>(columnName);
String modRowValue = "" + modRow.Field<Object>(columnName);
//check if they are the same
if (origRowValue.Equals(modRowValue))
{
//if they aren the same, increase the number matched by one
numberMatched++;
//add the column to the list of columns that don't match
}
else
{
mismatchedColumns.Add(columnName);
}
}
/* In the event it matches 15/16 columns, show the change */
if (numberMatched != numOfColumnsToMatch)
{
//Grab the shapeFile in question
Item differentAttrShpFile = origItemList[i];
//start blue highlighting
result += "<div class='turnBlue'>";
//show where the change was made at
result += "Change Detected at<br/> point X: " +
differentAttrShpFile.X + ",<br/> point Y: " +
differentAttrShpFile.Y + ",<br/>";
result += "</div>"; //end turnblue div
foreach (String mismatchedColumn in mismatchedColumns)
{
//iterate changes here
}
}
}
}
最佳答案
这类似于Loren所说的,但下面是.NET语言:)
1.重写GetHashCode方法以返回x,y,z和m的总和。重写等于方法以检查此总和。
2.在循环之前从modItemList(列表)迭代并创建HashSet。
3.在内部循环中,首先使用YourModHashSet.Contains(MyObject)方法检查HigSet中是否存在origItemList [i]。
4.如果.contains返回您为假,则携带一个,不匹配。
5.如果.Contains返回true,则遍历整个modItemList,并应用当前的逻辑检查整个列表的x,y,z和m。请注意,此处应使用列表,因为哈希表可能会吞噬哈希码相同的许多对象。
另外,我会使用Foreach而不是For,因为在这种情况下,我已经看到Foreach的结果稍差一些(快5%到30%)。
更新:
我创建了MyObject类,如下所示:
public class MyObject
{
public int X, Y, Z, M;
public override int GetHashCode()
{
return X*10000 + Y*100 + Z*10 + M;
}
public override bool Equals(object obj)
{
return (obj.GetHashCode() == this.GetHashCode());
}
}
GetHashCode方法在这里很重要。我们不希望有很多误报。当Hash匹配X,Y,Z和M的某些其他组合时,会出现误报。防止误报的最佳方法是将每个成员相乘,以使每个成员都影响HashCode的一位小数。请注意,您应该考虑不超过Int.Max值。如果X,Y,Z和M的期望值较小,则应该很好。
set2.Clear();
s1 = DateTime.Now;
MyObject matchingElement;
totalmatch = 0;
foreach (MyObject elem in list2)
set2.Add(elem);
foreach (MyObject t1 in list1)
{
if (set2.Contains(t1))
{
matchingElement = null;
foreach (MyObject t2 in list2)
{
if (t1.X == t2.X && t1.Y == t2.Y && t1.Z == t2.Z && t1.M == t2.M)
{
totalmatch++;
matchingElement = t2;
break;
}
}
//Do Something on matchingElement if not null
}
}
Console.WriteLine("set foreach with contains: " + (DateTime.Now - s1).TotalSeconds + "\t Total Match: " + totalmatch);
上面是我试图在答案中描述的示例代码。如果预期匹配次数较少,则此代码应能超级快速地工作。
关于c# - 嵌套循环C#中的循环优化,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16903709/