quandry is – which of the following two method performs best
Goal – get an object of type Wrapper ( defined below )
criteria – speed over storage
no. of records – about 1000- about 2000, max about 6K
Choices – Create Object on the fly or do a lookup from a dictionary
Execution speed – called x times per second
NB – i need to deliver the working code first and then go for optimization hence if any theorists can provide glimpses on behind the scene info, that’ll help before i get to the actual performance test possibly by eod thu
Definitions –
class Wrapper
{
public readonly DataRow Row;
public Wrapper(DataRow dr)
{
Row = dr;
}
public string ID { get { return Row["id"].ToString(); } }
public string ID2 { get { return Row["id2"].ToString(); } }
public string ID3 { get { return Row["id3"].ToString(); } }
public double Dbl1 { get { return (double)Row["dbl1"]; } }
// ... total about 12 such fields !
}
Dictionary<string,Wrapper> dictWrappers;
Method 1
Wrapper o = new Wrapper(dr);
/// some action with o
myMethod( o );
Method 2
Wrapper o;
if ( ! dictWrappers.TryGetValue( dr["id"].ToString(), out o ) )
{
o = new Wrapper(dr);
dictWrapper.Add(o.ID, o);
}
/// some action with o
myMethod( o );
The first one would be faster, since it isn’t actually doing a lookup, it is just doing a simple allocation and an assignment.
The two segments of code are not nearly equivalent. In function however, because Method 1 could create many duplicates.