Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Searching an ArrayList that contains a custom struct

Searching an ArrayList that contains a custom struct

Scheduled Pinned Locked Moved C#
asp-netsysadminalgorithmsquestionannouncement
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    shultas
    wrote on last edited by
    #1

    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

    G S 2 Replies Last reply
    0
    • S shultas

      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

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      Use a Hashtable with the filename as key. That should make it considerably faster to locate the items.

      --- b { font-weight: normal; }

      1 Reply Last reply
      0
      • S shultas

        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

        S Offline
        S Offline
        S Senthil Kumar
        wrote on last edited by
        #3

        What Guffa said. If you still want to continue with your approach, you could try 1. Does sFileListStruct have to be a struct/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)

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups