Reputation: 694
I have got two List<T>
objects. It is filled with objects of my own class iFile
, which contains a file path and the last edited date from the last call of the program.
Now I want to compare these two Lists, but my current code is running quite too slow! (4min for ~70.000 entries)
Here's my code:
private static List<iFile> compareLists(List<iFile> old)
{
List<iFile> cf = new List<iFile>();
foreach(iFile file in files)
{
bool notChanged = false;
iFile oldFile = files.Where(f => f.fPath == file.fPath).FirstOrDefault();
if(oldFile != null & oldFile.lastChange.Equals(file.lastChange))
{
notChanged = true;
}
if(!notChanged)
{
cf.Add(file);
}
}
return cf;
}
What would you suggest to change to get better performance results?
Upvotes: 1
Views: 81
Reputation: 236228
You can join files by fPath
. That will internally use hash set to find matches between two collections. Unlike simple Where
search which has complexity O(N), search in hash set has O(1) complexity:
var modifiedFiles = from file in files
join oldFile in old on file.fPath equals oldFile.fPath
where oldFile.lastChange != file.lastChange
select file;
return modifiedFiles.ToList();
Upvotes: 1