Array resizing?

I am wondering if the constant resizing of byte arrays can seriously affect performance. I am adding data to the class, and if the class already contains this data type, I need to add it to the existing byte array, which means that I will need to resize it. The problem is that there are some types of data that I have that will be added as a large amount, which means that several array sizes may occur.

Will this have a huge impact on performance? This class MAY be very performance critical.

If so, then I may have to carry out a major overhaul.

+4
source share
2 answers

If you want to resize, you should use List<byte> instead. Arrays cannot be changed, so you will need to create a completely new array and then copy the old content to the new array before adding additional content (this is what Array.Resize , if that's what you were talking about).

List<T> uses an array internally, but optimizes resizing, so you don't have to deal with it.

In fact, when the internal array is filled and new content is added, List<T> will double the size of the internal array, so resizing should be very rare - if you change the size of the array directly, on the other hand, you will either have to use a similar strategy and save the "counter sizing "or take the cost of resizing for any added content.

+9
source

Yes, it affects performance. The resize array does not just make the original array longer or shorter, it will create a new array and, if necessary, copy the data from the old to the new one.

As stated in the comment, use a dynamic container such as List or ArrayList, while the latter does not preserve the type, but is convenient to use, which I prefer. You can take a look at: http://msdn.microsoft.com/en-us/library/system.collections.arraylist.aspx

+2
source

Source: https://habr.com/ru/post/1400299/


All Articles