|
-
October 25th, 2006, 10:02 AM
#1
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
-
October 25th, 2006, 10:19 AM
#2
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.
-
October 25th, 2006, 10:22 AM
#3
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?
-
October 25th, 2006, 10:28 AM
#4
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.
-
October 25th, 2006, 10:31 AM
#5
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.
-
October 25th, 2006, 10:59 AM
#6
Re: Vector Retrieval Efficiency
-
October 25th, 2006, 11:08 AM
#7
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.
-
October 25th, 2006, 02:46 PM
#8
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).
-
October 26th, 2006, 01:26 PM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|