Need some fast code for some string array logic

D

David P. Donahue

I'm trying to come up with the best (fastest, as this code will be run
often) way to accomplish the following:

I have a dynamic string array X (as one grabbed from a GetFiles() or
GetDirectories() function call), which could have anywhere from 0 to
maybe 250 elements in it. I also have a statically-defined string array
Y which currently has 309 elements in it, but may grow over time. I
need to look through X and remove all elements that match any element in
Y. This can be accomplished through some nested loops and copying of
string arrays, but the inefficiency of that just doesn't sit right with me.

Is there anything in the .NET classes and functions that can make this
easier? Would it help if the arrays are known to be sorted? Does
anyone have any more creative algorithms that can do this quickly? Any
help would be much appreciated, thank you.


Regards,
David P. Donahue
(e-mail address removed)
 
B

Bob Grommes

Depending on usage patterns it might be better to store the info you are
pruning in a hashtable where the key and value are the same (or where the
value is the FileInfo object, if you need all that data along for the ride).
The speed of the hashed lookup would likely more than overcome the casting
overhead.

You can always copy the final result to an array and Array.Sort it if you
need it in a particular order.

Better still, under CLR 2.0 you could use Dictionary<string> instead of a
Hashtable, and then you won't have the casting penalty either.

Obviously your mileage may vary -- I would put together a few proof of
concept variations and benchmark them.

--Bob
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Well, you could store Y sorted , that will help a little , even maybe store
it in a b-tree will further improve the efficiency of the search.

The constrain you have is the X array , as you have no control over it. so
you will have to do a linear search on it so you should concentrate your
optimization in Y instead.

Don't think you have much more options here :(

cheers,
 
D

David P. Donahue

I guess I need to stop thinking so 1-dimensionally :) Hashtables make
the whole thing that much easier, thanks!


Regards,
David P. Donahue
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top