Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resizing array performance?

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.

like image 912
Noah Roth Avatar asked Mar 07 '12 22:03

Noah Roth


People also ask

How to resize a multi-dimensional array in Java?

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.

How to avoid dynamic array resizing performance penalties in MATLAB?

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.

What happens when the size of the array enlargement element increases?

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.

What happens if newsize is less than the length of array?

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.


1 Answers

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.

like image 180
BrokenGlass Avatar answered Nov 15 '22 06:11

BrokenGlass