Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a DataTable consume more memory than a List<T>?

Is there a trade off in respect to performance, trade off in respect to memory consumption?

like image 467
David Robbins Avatar asked Nov 08 '08 21:11

David Robbins


2 Answers

Yes, absolutely a DataTable consumes more memory than a List.

The DataTable class has lots of objects for column definitions, the table info (name, etc), collections of row objects, the "item arrays" for each row (which is basically all the List would have), etc.

EDIT: Also, List is more performant for adding items, itterating through, etc. (reflect the code for "Add" for generic lists, and for data tables to see more detail).

like image 166
Timothy Khouri Avatar answered Oct 20 '22 11:10

Timothy Khouri


Oh yeah, it's fat. It can be much more efficient than List<> though. It creates an index so lookups can be O(1). Rows are stored in a red-black tree so inserts and deletes can be O(log n). All these operations are O(n) for List<>. To get this kind of perf, you'll have to choose your columns and queries wisely though. Same kind of considerations as a regular database table.

like image 43
Hans Passant Avatar answered Oct 20 '22 13:10

Hans Passant