views:

84

answers:

1

Hello, colleagues. I've got task to fast find object by its string property. Object:

  class DicDomain
  {
    public virtual string Id{ get; set; }
    public virtual string Name { get; set; }
  }

For storing my object I use List[T] dictionary where T is DicDomain for now . I've got 5-10 such lists, which contain about 500-20000 at each one. Task is find objects by its Name. I use next code now:

  List<T> entities = dictionary.FindAll(s => s.Name.Equals(word, StringComparison.OrdinalIgnoreCase));

I've got some questions:

Is my search speed optimal. I think now.

  1. Data structure. It List good for this task. What about hashtable,sorted...
  2. Method Find. May be i should use string intern??

I haven't much exp at these tasks. Can u give me good advice for increase perfomance. Thanks

+5  A: 

If you are performing this operation frequently, you can build a Dictionary<string, List<DicDomain>> (or Dictionary<string, DicDomain> if Name is unique) to build a reverse mapping (from a name to a bunch of DicDomain objects) and keep that dictionary up to date.

The task will be a simple hash table lookup after that:

 var list = dictionary[name];
Mehrdad Afshari
Frequently 100000 times every day. Thanks for advice!!! I can't imagine such nice way. And what about Dictionary vs HashTable?
Andrew Kalashnikov
@Andrew: Short answer: `Dictionary`. Long answer: http://stackoverflow.com/questions/1089132/net-hashtable-vs-dictionary-can-the-dictionary-be-as-fast/1089142#1089142
Mehrdad Afshari
StringComparison.OrdinalIgnoreCase and what about this. How can i implement CaseInsensetive search?
Andrew Kalashnikov
@Andrew: You can pass `StringComparer.OrdinalIgnoreCase` to the `Dictionary` constructor to control the way keys are compared.
Mehrdad Afshari
Thanks. Speed is great now!!!
Andrew Kalashnikov