9

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.

6
  • 1
    You should see if it's taking up a large time to execute relative to other parts of your code by using a profiler. VS has one built in, there's some free ones out there, or paid ones like ANTS or dotTrace. Commented Mar 7, 2012 at 22:43
  • Build it, profile it, if it's too slow, refactor it. Most likely the places you think will be the most performance-critical will actually not be the most performance-critical places :) Commented Mar 7, 2012 at 22:44
  • @JayOtterbein I've actually never bothered to mess around with a profiler, it might be a good idea for me to start, though. Commented Mar 7, 2012 at 22:45
  • 1
    @Alex, if you did not use a profiler or measured performance of the code in some other way you should not claim the code is performance critical. As said in answers you should use List<T> instead of Array, but for readability/correctness reasons, not performance reasons. List<T> already written/tested to support resizing unlike hand made code with Array (no matter how good you are). Commented Mar 7, 2012 at 23:37
  • 3
    If you want to know whether a particular line of code adversely affects performance then set a target that is acceptable to your customer, measure the performance of the code, and see if it beats the target. If it does, great, you're done. If it does not then go get a profiler, find the slowest thing, and fix it. Asking strangers on the internet who do not know either your code or your customer is taking time away from time you could be spending writing performance tests. Commented Dec 4, 2013 at 17:01

2 Answers 2

12

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.

Sign up to request clarification or add additional context in comments.

Comments

3

Yes it is impacting performance. Resize array does not simply make the original array longer or shorter, it will create a new array and copy the data from old to new if necessary.

As suggested in the comment, use dynamic container such as List or ArrayList, while the latter is not type save 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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.