The code below is extremely slow for tables of any significant size. (100, 1000, etc.). The criminal copies my objects using new T() . Please note that this is not my final code, I just broke parts of it to get a simpler profile. Ignoring and initializing happen together as soon as I reorganize the code back into the form.
Is there any way to speed this up? I probably forget something really simple, or maybe I'm the backbone. I hope the first one.
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); }
for more information:
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 this is the only class task to provide the set values.
edit for more information:
Since this seems to be a hot topic, here is what my method looks like for realities, before deducing the 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; }
performance c #
Samantha branham
source share