Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# DataTable vs Some generic collection performance

I know that the DataTable is using boxing/unboxing when we are inserting/getting the data. If we have a really big amount of data containing only int's for example, isn't it faster to use some sort of generic DataTable , let's say MyDataTable<int>, which will result in avoiding boxing/unboxing overhead?

like image 867
Dimitar Tsonev Avatar asked Dec 12 '25 08:12

Dimitar Tsonev


1 Answers

Strings are reference-types, so there is no boxing overhead: they are never boxed. DataTable does have plenty of overheads though - that is the cost of being able to represent arbitrary column models along with constraints, change-tracking, etc. For optimum performance, a POCO model is hard to beat. For example:

public class Customer {
    public int Id { get; set; }
    public string Name { get; set; }
    // ...
}

There are plenty of ORMs and micro-ORMs that make working with this type of model.

Note, however, that in DataTable, the values are stored in correctly-typed arrays. Ints are stored in an int[] - which is does by storing data in columns rather than rows. Boxing only happens when getting data into/out-of the DataTable.

like image 58
Marc Gravell Avatar answered Dec 15 '25 11:12

Marc Gravell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!