title: Loop optimization within a nested loop c#
body: <p>I am comparing two lists of data that were generated from a binary file. I have a good idea on why it's running slow, when there's a significant amount of records, it does un-needed redundant work. </p>
  
  <p>For example, if a1 = a1, condition is true. Since 2a != 1a so why even bother checking it? I need to eliminate 1a from being checked again. If I don't, it will check the first record when it goes to check the 400,000th record. I thought about making the second for loop a foreach, but I can't remove 1a while iterating through the nested loop</p>
  
  <p>The amount of items that can be in either 'for loop' can vary. I don't think a single for loop using 'i' will work since the match can be anywhere. I'm reading from a binary file</p>
  
  <p>This is my current code. Program has been running for over an hour, and it's still going. I removed a lot of my iterating code for readability reasons.</p>
  
  <pre><code>   for (int i = 0; i &lt; origItemList.Count; i++)
      {
          int modFoundIndex = 0;
          Boolean foundIt = false;
          for (int g = 0; g &lt; modItemList.Count; g++)
          {
              if ((origItemList[i].X == modItemList[g].X)
                  &amp;&amp; (origItemList[i].Y == modItemList[g].Y)
                  &amp;&amp; (origItemList[i].Z == modItemList[g].Z)
                  &amp;&amp; (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&lt;String&gt; mismatchedColumns = new List&lt;String&gt;();
  
              //check each column name for a change
              foreach (String columnName in columnNames)
              {
                  //this grabs whatever value is in that field                            
                  String origRowValue = "" + origRow.Field&lt;Object&gt;(columnName);
                  String modRowValue = "" + modRow.Field&lt;Object&gt;(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 += "&lt;div class='turnBlue'&gt;";
                  //show where the change was made at
                  result += "Change Detected at&lt;br/&gt; point X: " +
                  differentAttrShpFile.X + ",&lt;br/&gt; point Y: " +
                      differentAttrShpFile.Y + ",&lt;br/&gt;";
                  result += "&lt;/div&gt;"; //end turnblue div
                  foreach (String mismatchedColumn in mismatchedColumns)
                  {
                      //iterate changes here
  
                  }
  
              }
  
          }
  
      }
  </code></pre>