The code below is extremely slow for tables of any significant size. (100, 1000, etc...) The culprit is instantiating my objects with new T()
. Note that this isn't my finalized code, I've just broken parts of it out in order to more easily profile. Instantiation and initialization will happen together once I refactor the code back into shape.
Is there any way to speed this up? I'm probably forgetting something really simple, or maybe I'm boned. Hopefully, the former.
public static IList<T> ToList<T>(this DataTable table) where T : Model, new()
{
T[] entities = new T[table.Rows.Count];
// THIS LOOP IS VERY VERY SLOW
for (int i = 0; i < table.Rows.Count; i++)
entities[i] = new T();
// THIS LOOP IS FAST
for (int i = 0; i < table.Rows.Count; i++)
entities[i].Init(table, table.Rows[i]);
return new List<T>(entities);
}
edit for more info:
The constructor of any given ModelType
will look like this:
public ModelType()
{
_modelInfo = new ModelTypeInfo();
}
The constructor of any given ModelTypeInfo
will simply set some string and string[] values, and that class' only job is to provide the values set.
edit for even more info:
Since it seems to be a hot topic, here is what my method looks like for reals before breaking out object construction and initialization:
public static IList<T> ToList<T>(this DataTable table, ModelInfo modelInfo) where T : Model, new()
{
var tempRepository = new Repository<T>(modelInfo);
var list = new List<T>();
foreach (DataRow row in table.Rows)
list.Add(tempRepository.FromData(table, row));
return list;
}