I'm wondering if constantly resizing byte arrays can take a big hit on performance. I'm adding data to a class and if the class already contains that data type I need to add it to the existing byte array, which means I'll need to resize it. The problem is that there are some data types that I have that will be adding in bulk which means there could be multiple array resizes occurring.
Would this make a huge impact on performance? This class CAN be very performance critical.
If it does, then I might have to do a design-overhaul.
This method is an O ( n) operation, where n is newSize. The Resize method resizes a one-dimensional array only. The Array class does not include a method for resizing multi-dimensional arrays. To do this, you must either provide your own code or call a special-purpose method in a third-party library.
As I have explained last week, the best way to avoid the performance penalties associated with dynamic array resizing (typically, growth) in Matlab is to pre-allocate the array to its expected final size. I have shown different alternatives for such preallocation, but in all cases the performance is much better than using a naïve dynamic resize.
As the size of the array enlargement element (in this case, a 3×3 matrix) increases, the computer needs to allocate more memory space more frequently, thereby increasing execution time and the importance of preallocation.
If newSize is less than the Length of the old array, a new array is allocated and elements are copied from the old array to the new one until the new one is filled; the rest of the elements in the old array are ignored. If newSize is equal to the Length of the old array, this method does nothing.
If resizing is required then you should use a List<byte>
instead. Arrays cannot be resized so you would have to create a completely new array and then copy the old content into the new array before adding additional content (this is what Array.Resize
does if that's what you were referring to).
List<T>
is using an array internally but will optimize resizing so you don't have to deal with it.
Essentially once the internal array is full and new content is added, List<T>
will double the internal array size, hence resizing should in fact occur very rarely - if you resize your array directly on the other hand you will either have to employ a similar strategy and keep a "size counter" or take the resize performance cost on any content addition.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With