tags:

views:

61

answers:

4

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

+3  A: 

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
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
A: 

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
know of any resouces avalible that detail the method
fishhead
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<EquipStatus,bool>. 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
A: 

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
is that availible in .net 2.0
fishhead
Okay, that wasn't clear from your question. Sadly, this is at least .net3.5 and c#3. I retaged your question so it's more clearly visible.
Femaref
A: 

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.

Wallacoloo
ah yes!...would we expect big returns on this?
fishhead