Searching an ArrayList that contains a custom struct
-
Hi All, I have the following: public struct sFileListStruct { public String sFileName; public DateTime dFileTime; } I am developing an application that backs newly modified files up to the server. I want to save bandwidth, and the route that I have chosen is when the application copies a file onto the server, it will add an entry into an arraylist. At the end of the operation the arraylist is written to a file. (Serialized). When the application starts, it reads the file and populates the arraylist with sFileListStruct. These are the files that have been backed up already. Next, I perform a file search and populate another ArrayList. This second ArrayList contains all the current files. I want to compare the filename's and then look at the modification time for each file. I basically have something such as foreach (sFileListStruct sCurrentFile in aAllFilesArrayList) { // Do some code in here } Now, what I have tried inside of the foreach is to do this: if (aFilesCopiedArrayList.IndexOf(sCurrentFile) != -1) { // It's a new file, or the modification date has changed, do something here } I have also tried using sFilesCopiedArrayList.Contains(). They are incredibly slow. There are around 1,000 files that it is comparing, and it takes 60 seconds or more to do a comparison of them using either of those methods (This is a 2.6GHZ dual core machine). What I have winded up doing, which cuts that time down to about 10 seconds is actually putting a foreach within a foreach foreach (sFileListStruct sCurrentFile in aAllFilesArrayList) { foreach (sFileListStruct sOldFile in aOldFilesArrayList) { if ((sOldFile.sFileName == sCurrentFile.sFileName) && (sCurrentFile.dFileTime > sOldFile.dFileTime) { // copy and update... } } } That appears to be a lot quicker than using IndexOf or Contains. I am wondering if anyone knows why, or is there some other way that I can check one ArrayList's contents against another ArrayList's contents quicker? I am just trying to do it the proper way and the way that will allow it to run the quickest. TIA
-
Hi All, I have the following: public struct sFileListStruct { public String sFileName; public DateTime dFileTime; } I am developing an application that backs newly modified files up to the server. I want to save bandwidth, and the route that I have chosen is when the application copies a file onto the server, it will add an entry into an arraylist. At the end of the operation the arraylist is written to a file. (Serialized). When the application starts, it reads the file and populates the arraylist with sFileListStruct. These are the files that have been backed up already. Next, I perform a file search and populate another ArrayList. This second ArrayList contains all the current files. I want to compare the filename's and then look at the modification time for each file. I basically have something such as foreach (sFileListStruct sCurrentFile in aAllFilesArrayList) { // Do some code in here } Now, what I have tried inside of the foreach is to do this: if (aFilesCopiedArrayList.IndexOf(sCurrentFile) != -1) { // It's a new file, or the modification date has changed, do something here } I have also tried using sFilesCopiedArrayList.Contains(). They are incredibly slow. There are around 1,000 files that it is comparing, and it takes 60 seconds or more to do a comparison of them using either of those methods (This is a 2.6GHZ dual core machine). What I have winded up doing, which cuts that time down to about 10 seconds is actually putting a foreach within a foreach foreach (sFileListStruct sCurrentFile in aAllFilesArrayList) { foreach (sFileListStruct sOldFile in aOldFilesArrayList) { if ((sOldFile.sFileName == sCurrentFile.sFileName) && (sCurrentFile.dFileTime > sOldFile.dFileTime) { // copy and update... } } } That appears to be a lot quicker than using IndexOf or Contains. I am wondering if anyone knows why, or is there some other way that I can check one ArrayList's contents against another ArrayList's contents quicker? I am just trying to do it the proper way and the way that will allow it to run the quickest. TIA
-
Hi All, I have the following: public struct sFileListStruct { public String sFileName; public DateTime dFileTime; } I am developing an application that backs newly modified files up to the server. I want to save bandwidth, and the route that I have chosen is when the application copies a file onto the server, it will add an entry into an arraylist. At the end of the operation the arraylist is written to a file. (Serialized). When the application starts, it reads the file and populates the arraylist with sFileListStruct. These are the files that have been backed up already. Next, I perform a file search and populate another ArrayList. This second ArrayList contains all the current files. I want to compare the filename's and then look at the modification time for each file. I basically have something such as foreach (sFileListStruct sCurrentFile in aAllFilesArrayList) { // Do some code in here } Now, what I have tried inside of the foreach is to do this: if (aFilesCopiedArrayList.IndexOf(sCurrentFile) != -1) { // It's a new file, or the modification date has changed, do something here } I have also tried using sFilesCopiedArrayList.Contains(). They are incredibly slow. There are around 1,000 files that it is comparing, and it takes 60 seconds or more to do a comparison of them using either of those methods (This is a 2.6GHZ dual core machine). What I have winded up doing, which cuts that time down to about 10 seconds is actually putting a foreach within a foreach foreach (sFileListStruct sCurrentFile in aAllFilesArrayList) { foreach (sFileListStruct sOldFile in aOldFilesArrayList) { if ((sOldFile.sFileName == sCurrentFile.sFileName) && (sCurrentFile.dFileTime > sOldFile.dFileTime) { // copy and update... } } } That appears to be a lot quicker than using IndexOf or Contains. I am wondering if anyone knows why, or is there some other way that I can check one ArrayList's contents against another ArrayList's contents quicker? I am just trying to do it the proper way and the way that will allow it to run the quickest. TIA
What Guffa said. If you still want to continue with your approach, you could try 1. Does
sFileListStruct
have to be astruct/code>? Things would be much faster if it's a `class` instead. 2. converting the ArrayList to List<sFileListStruct>. This will work only if you're using .NET 2.0. This saves the unboxing cost when you read the struct out of the list. 3. Overriding Equals in your struct should help things too. I've read that the default implementation of Equals, which is what IndexOf uses, uses reflection to compare field values. Regards Senthil [MVP - Visual C#] _____________________________ [My Blog](http://blogs.wdevs.com/senthilkumar) | [My Articles](http://www.codeproject.com/script/articles/list_articles.asp?userid=492196) | [My Flickr](http://www.flickr.com/photos/senthilkumar/) | [WinMacro](http://geocities.com/win_macro)