1

At some time there will be a large amount of records, about 50,000. with that in mind is the method GetEquipmentRecord up to the task. thanks for you opinions.

c# ,net 2,0

public enum EquipShift { day, night };

public class EquipStatusList : List<EquipStatus>
{
    string SerialFormat = "yyyyMMdd";

    int _EquipmentID;
    string _DateSerial;
    EquipShift _Shift;

    public EquipStatus GetEquipmentRecord(int equipmentID, EquipShift shift, 
                                            DateTime date)
    {
        _DateSerial = date.ToString(SerialFormat);
        _Shift = shift;
        _EquipmentID = equipmentID;

        return this.Find(checkforEquipRecord);
    }

    bool checkforEquipRecord(EquipStatus equip)
    {
        if ((equip.EquipmentID == _EquipmentID)
              && (equip.Shift == _Shift) 
              && (equip.Date.ToString(SerialFormat) == _DateSerial))
            return true;
        else
            return false;
    }
}

update : I have changed the evaluation to read

           if ((equip.Date.Date == _date.Date) &&  (equip.EquipmentID == _EquipmentID) && (equip.Shift == _Shift)  )

not sure it that helps

fishhead
  • 5,829
  • 7
  • 32
  • 43

4 Answers4

3

Without commenting on your choice of algorithm, we can say that it probably is optimized enough.

You've got an O(n) find() in there; searching a sorted list with a binary search would be O(lg n) and searching a hash-set (or Dictionary in C# 2.0) would be O(1) for example. Hash-set would obviously be the way to go if you were calling this function often.

But bottlenecks are rarely where you expect them, so that you ask the question on this particular instance means that, on balance, profiling later will actually show that the big slowdowns are elsewhere.

Will
  • 73,905
  • 40
  • 169
  • 246
  • thanks for the comment...I am looking that perhaps the code evaluate for a date first and then bail if that's not in scope. Being new to this I am pretty much in unchartered territory all the time. – fishhead Jun 06 '10 at 22:26
0

You could speed this up considerably by implementing a suitable GetHashCode method and using a System.Collections.Generic.HashSet<EquipStatus> as the backing container. However, as it's not entirely clear how you are using your class (i.e. which other List<T> methods you use), ymmv.

spender
  • 117,338
  • 33
  • 229
  • 351
  • know of any resouces avalible that detail the method – fishhead Jun 06 '10 at 22:36
  • Hmm. Are you using .net 2.0? If so HashSet isn't available. However, if the small extra amount of storage isn't an issue, you can always use the keys of a Dictionary. This question outlines ins and outs of GetHashCode... http://stackoverflow.com/questions/1378686/general-advice-and-guidelines-on-how-to-properly-override-object-gethashcode – spender Jun 06 '10 at 22:43
0

No, it is not. Your whole construct is not able to be used in a multitasking environment. You are storing the details to search for as instance members of the class. I would take advantage of PLINQ (Parallel Linq) and the usual operators, also I wouldn't derive from the List itself, but offer an extension method like this:

public static EquipStatus GetEquipmentRecord(this IEnumerable<EquipStatus> list, int equipmentID, EquipShift shift, DateTime date)
{
  return list.AsParallel().FirstOrDefault(e => e.EquipmentID == equipmentID && e.Shift == shift, e.Date.Date == date.Date);
}

By this, multiple searches at the same time are possible.

Femaref
  • 60,705
  • 7
  • 138
  • 176
0

well an obvious way to improve your checkForEquipRecord method is to change

if ((equip.Date.Date == _date.Date) &&  (equip.EquipmentID == _EquipmentID) && (equip.Shift == _Shift)  )
    return true;
else
    return false;

to just return (equip.Date.Date == _date.Date) && (equip.EquipmentID == _EquipmentID) && (equip.Shift == _Shift)

As far as efficiency goes, it might already be an optimization that the JIT compiler makes.

Ponkadoodle
  • 5,777
  • 5
  • 38
  • 62