CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Feb 2006
    Posts
    78

    Vector Retrieval Efficiency

    I am working in a program that was handed off to me. I need to make it more efficient when processing files. There is a section that has two for loops, one is nested inside the other. Code is below. The Vector can be up to 20,000 items. In this code, for each item in the DataVal, it checks every item in SetupData, and if they match then it processes, and I have now added a break out of the nested loop which helps. Does anyone have a better suggestion for doing this? Thanks.

    for (int a=0; a < DataVal.size(); a++)
    {
    Hashtable dHt = (Hashtable)DataVal.elementAt(a);

    int dX = Integer.parseInt((String) dHt.get("X")) + xOffset;
    int dY = Integer.parseInt((String) dHt.get("Y")) + yOffset;

    if ((dX <= 0) || (dY <= 0))
    {
    DataVal.removeElementAt(a);
    a--;
    continue;
    } // end if

    boolean keep = false;
    for (int b=0; b < SetupData.size(); b++)
    {
    Hashtable tHt = (Hashtable)SetupData.elementAt(b);

    int tX = Integer.parseInt((String) tHt.get("X"));
    int tY = Integer.parseInt((String) tHt.get("Y"));

    String tType = (String) tHt.get("T_TYPE");

    if ((dX == tX) && (dY == tY))
    {
    if (dHt.containsKey("X"))
    {
    dHt.remove("X");
    } // end if
    if (dHt.containsKey("Y"))
    {
    dHt.remove("Y");
    } // end if
    if (dHt.containsKey("T_TYPE"))
    {
    dHt.remove("T_TYPE");
    } // end if

    dHt.put("X",""+tX);
    dHt.put("Y",""+tY);
    dHt.put("T_TYPE",tType);

    keep = true;
    break;
    } // end if
    } // end for

    if (!keep)
    {
    DataVal.removeElementAt(a);
    a--;
    continue;
    } // end if
    else
    {
    DataVal.setElementAt(dfabHt, a);
    } // end else

    } // end for

  2. #2
    Join Date
    Apr 2001
    Location
    South Africa, Jo'burg
    Posts
    680

    Re: Vector Retrieval Efficiency

    Hi,

    Try using ArrayList instead of Vector if you DON'T need synchronization. Vectors are synchronised and ArrayLists aren't, this means that if you don't need the List to be synchronised you are gaining a lot of overhead for nothing. If only one thread is accessing those Vectors consider using ArrayList instead.

    Hope This Helps
    Byron Tymvios

    Please use [ CODE ] and [/ CODE ] tags when posting code! See THIS on how to use code tags.

  3. #3
    Join Date
    Feb 2006
    Posts
    78

    Re: Vector Retrieval Efficiency

    Thanks! I have been looking at the ArrayList and possibly moving to that. What exactly do you mean by 'synchronized'? Is that due to multiple threads hitting the vector?

  4. #4
    Join Date
    Apr 2001
    Location
    South Africa, Jo'burg
    Posts
    680

    Re: Vector Retrieval Efficiency

    When a code block is labeled as synchronised then only one thread can execute the containing code at a time, so there is a lock on the synchronised block. If other threads are also trying to execute that code they will be queued and then the next thread will be able to access it once the first thread has finished executing the code and released the lock.

    So you should only use Vector if multiple threads are going to be hitting it, if not go with ArrayList.
    Byron Tymvios

    Please use [ CODE ] and [/ CODE ] tags when posting code! See THIS on how to use code tags.

  5. #5
    Join Date
    Apr 2001
    Location
    South Africa, Jo'burg
    Posts
    680

    Re: Vector Retrieval Efficiency

    And please use code tags when posting code, it makes it a whole lot easier to read. I actually never went over your code because it hurts my eyes at the moment. So if you want more help with your code, don't forget the code tags.
    Byron Tymvios

    Please use [ CODE ] and [/ CODE ] tags when posting code! See THIS on how to use code tags.

  6. #6
    Join Date
    Feb 2006
    Posts
    78

    Re: Vector Retrieval Efficiency

    Thanks! Code tags?

  7. #7
    Join Date
    Apr 2001
    Location
    South Africa, Jo'burg
    Posts
    680

    Re: Vector Retrieval Efficiency

    Click on the link in my signature to see how to use them. They format your code so we can read it, otherwise your code is impossible to read.
    Byron Tymvios

    Please use [ CODE ] and [/ CODE ] tags when posting code! See THIS on how to use code tags.

  8. #8
    Join Date
    Sep 2005
    Location
    Cluj-Napoca, Romania
    Posts
    161

    Re: Vector Retrieval Efficiency

    you don't need all that remove from the Hashtable stuff, because you replace those values anyway when you do dHt.put("X",""+tX).

  9. #9
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Vector Retrieval Efficiency

    Code:
    dHt.put("X",""+tX);
    Converting an int to a String by concatenating it with an empty string is not efficient you should use the String.valueOf() method, eg
    Code:
    dHt.put("X", String.valueOf(tx);
    Better still, given that you are converting the values back to ints earlier in the code, store the int values as Integers rather than as Strings, then you don't have the overhead of converting Strings to ints.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured