3

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

Andrew Kalashnikov
  • 3,073
  • 5
  • 34
  • 57

1 Answers1

5

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
  • 414,610
  • 91
  • 852
  • 789