<p>I am trying to compare two large datasets from a SQL query. Right now the SQL query is done externally and the results from each dataset is saved into its own csv file. My little C# console application loads up the two text/csv files and compares them for differences and saves the differences to a text file.</p>

<p>Its a very simple application that just loads all the data from the first file into an arraylist and does a .compare() on the arraylist as each line is read from the second csv file. Then saves the records that don't match.</p>

<p>The application works but I would like to improve the performance. I figure I can greatly improve performance if I can take advantage of the fact that both files are sorted, but I don't know a datatype in C# that keeps order and would allow me to select a specific position. Theres a basic array, but I don't know how many items are going to be in each list. I could have over a million records. Is there a data type available that I should be looking at? </p>

[{'answer_id': 77546, 'author': 'MagicKat', 'author_id': 8505, 'author_profile': 'https://Stackoverflow.com/users/8505', 'pm_score': 0, 'selected': False, 'text': '<p>Well, there are several approaches that would work.  You could write your own data structure that did this.  Or you can try and use SortedList.  You can also return the DataSets in code, and then use .Select() on the table.  Granted, you would have to do this on both tables.</p>\n'}, {'answer_id': 77551, 'author': 'Sam', 'author_id': 9406, 'author_profile': 'https://Stackoverflow.com/users/9406', 'pm_score': 0, 'selected': False, 'text': '<p>You can easily use a SortedList to do fast lookups. If the data you are loading is already sorted, insertions into the SortedList should not be slow.</p>\n'}, {'answer_id': 77567, 'author': 'Mitchel Sellers', 'author_id': 13279, 'author_profile': 'https://Stackoverflow.com/users/13279', 'pm_score': 0, 'selected': False, 'text': '<p>If you are looking simply to see if all lines in FileA are included in FileB you could read it in and just compare streams inside a loop.</p>\n\n<p>File 1\nEntry1\nEntry2\nEntry3</p>\n\n<p>File 2\nEntry1\nEntry3</p>\n\n<p>You could loop through with two counters and find omissions, going line by line through each file and see if you get what you need.</p>\n'}, {'answer_id': 77570, 'author': 'Arno', 'author_id': 13685, 'author_profile': 'https://Stackoverflow.com/users/13685', 'pm_score': 0, 'selected': False, 'text': '<p>Maybe I misunderstand, but the ArrayList will maintain its elements in the same order by which you added them. This means you can compare the two ArrayLists within one pass only - just increment the two scanning indices according to the comparison results.</p>\n'}, {'answer_id': 77574, 'author': 'Shane Courtrille', 'author_id': 12503, 'author_profile': 'https://Stackoverflow.com/users/12503', 'pm_score': 0, 'selected': False, 'text': '<p>One question I have is have you considered "out-sourcing" your comparison.  There are plenty of good diff tools that you could just call out to.  I\'d be surprised if there wasn\'t one that let you specify two files and get only the differences.  Just a thought.</p>\n'}, {'answer_id': 77588, 'author': 'cranley', 'author_id': 10308, 'author_profile': 'https://Stackoverflow.com/users/10308', 'pm_score': 1, 'selected': False, 'text': "<p>System.Collections.Specialized.StringCollection allows you to add a range of values and, using the .IndexOf(string) method, allows you to retrieve the index of that item.</p>\n\n<p>That being said, you could likely just load up a couple of byte[] from a filestream and do byte comparison... don't even worry about loading that stuff into a formal datastructure like StringCollection or string[]; if all you're doing is checking for differences, and you want speed, I would wreckon byte differences are where it's at.</p>\n"}, {'answer_id': 77617, 'author': 'David J. Sokol', 'author_id': 1390, 'author_profile': 'https://Stackoverflow.com/users/1390', 'pm_score': 2, 'selected': False, 'text': '<p>If data in both of your CSV files is already sorted and have the same number of records, you could skip the data structure entirely and do in-place analysis.</p>\n\n<pre><code>StreamReader one = new StreamReader("C:\\file1.csv");\nStreamReader two = new StreamReader("C:\\file2.csv");\nString lineOne;\nString lineTwo;\n\nStreamWriter differences = new StreamWriter("Output.csv");\nwhile (!one.EndOfStream)\n{\n    lineOne = one.ReadLine();\n    lineTwo = two.ReadLine();\n    // do your comparison.\n    bool areDifferent = true;\n\n    if (areDifferent)\n        differences.WriteLine(lineOne + lineTwo);\n}\n\none.Close();\ntwo.Close();\ndifferences.Close();\n</code></pre>\n'}, {'answer_id': 77823, 'author': 'skb', 'author_id': 14101, 'author_profile': 'https://Stackoverflow.com/users/14101', 'pm_score': 0, 'selected': False, 'text': '<p>I think the reason everyone has so many different answers is that you haven\'t quite got your problem specified well enough to be answered.  First off, it depends what kind of differences you want to track.  Are you wanting the differences to be output like in a WinDiff where the first file is the "original" and second file is the "modified" so you can list changes as INSERT, UPDATE or DELETE?  Do you have a primary key that will allow you to match up two lines as different versions of the same record (when fields other than the primary key are different)?  Or is is this some sort of reconciliation where you just want your difference output to say something like "RECORD IN FILE 1 AND NOT FILE 2"?</p>\n\n<p>I think the asnwers to these questions will help everyone to give you a suitable answer to your problem.</p>\n'}, {'answer_id': 77901, 'author': 'Jason Jackson', 'author_id': 13103, 'author_profile': 'https://Stackoverflow.com/users/13103', 'pm_score': 0, 'selected': False, 'text': '<p>If you have two files that are each a million lines as mentioned in your post, you might be using up <em>a lot</em> of memory.  Some of the performance problem might be that you are swapping from disk.  If you are simply comparing line 1 of file A to line one of file B, line2 file A -> line 2 file B, etc, I would recommend a technique that does not store so much in memory.  You could either read write off of two file streams as a previous commenter posted and write out your results "in real time" as you find them.  This would not explicitly store anything in memory.  You could also dump chunks of each file into memory, say one thousand lines at a time, into something like a List.  This could be fine tuned to meet your needs.</p>\n'}, {'answer_id': 77932, 'author': 'Jonathan Rupp', 'author_id': 12502, 'author_profile': 'https://Stackoverflow.com/users/12502', 'pm_score': 1, 'selected': False, 'text': '<p>This is an adaptation of David Sokol\'s code to work with varying number of lines, outputing the lines that are in one file but not the other:</p>\n\n<pre><code>StreamReader one = new StreamReader("C:\\file1.csv");\nStreamReader two = new StreamReader("C:\\file2.csv");\nString lineOne;\nString lineTwo;\nStreamWriter differences = new StreamWriter("Output.csv");\nlineOne = one.ReadLine();\nlineTwo = two.ReadLine();\nwhile (!one.EndOfStream || !two.EndOfStream)\n{\n  if(lineOne == lineTwo)\n  {\n    // lines match, read next line from each and continue\n    lineOne = one.ReadLine();\n    lineTwo = two.ReadLine();\n    continue;\n  }\n  if(two.EndOfStream || lineOne &lt; lineTwo)\n  {\n    differences.WriteLine(lineOne);\n    lineOne = one.ReadLine();\n  }\n  if(one.EndOfStream || lineTwo &lt; lineOne)\n  {\n    differences.WriteLine(lineTwo);\n    lineTwo = two.ReadLine();\n  }\n}\n</code></pre>\n\n<p>Standard caveat about code written off the top of my head applies -- you may need to special-case running out of lines in one while the other still has lines, but I think this basic approach should do what you\'re looking for.</p>\n'}, {'answer_id': 92833, 'author': 'Shane Courtrille', 'author_id': 12503, 'author_profile': 'https://Stackoverflow.com/users/12503', 'pm_score': 0, 'selected': False, 'text': "<p>To resolve question #1 I'd recommend looking into creating a hash of each line.  That way you can compare hashes quick and easy using a dictionary. </p>\n\n<p>To resolve question #2 one quick and dirty solution would be to use an IDictionary.  Using itemId as your first string type and the rest of the line as your second string type. You can then quickly find if an itemId exists and compare the lines.  This of course assumes .Net 2.0+</p>\n"}]
['https://Stackoverflow.com/questions/77503', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/8664/']