Joudicek Jouda
Joudicek Jouda

Reputation: 792

How to effectively discard an array and fill it with new values?

I've got a few global arrays I use in a simple WinForms game. The arrays are initialized when a new game starts. When a player is in the middle of the game (the arrays are filled with data) he clicks on the StartNewGame() button (restarts the game). What to do next?

Is it ok to reinitialize the whole array for the new game or should I just set every array item to null and use the already initialized array (which would be slower)?

I mean is it okay to do something like this?

MyClass[,] gameObjects;

public Form1()
{
   StartNewGame();

   // game flow .. simplified here .. normally devided in functions and events.. 

   StartNewGame();

   // other game flow
}

public StartNewGame()
{
   gameObjects = new MyClass[10,10];

   // some work with gameObjects
}

Upvotes: 2

Views: 143

Answers (4)

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112342

gameObjects = new MyClass[10,10];

... is the way to go. This is definitely faster than looping through the array and setting the items to null. It is also simpler to code and to understand. But both variants are very fast in anyway, unless you have tens of millions of entries! '[10, 10]' is very small, so forget about performance and do what seems more appropriate and more understandable to you. A clean coding is more important than performance in most cases.

Upvotes: 0

Porkbutts
Porkbutts

Reputation: 964

If there are only 100 elements as in your example, then there shouldn't really be a noticeable performance hit.

If you reinitialize the array, you will perform n constructions for n objects. The garbage collector will come clean up the old array and de-allocate those old n objects at some later time. (So you have n allocations upfront, and n deallocations by the GC).

If you set each pointer in the array to null, the garbage collector will still do the same amount of work and come clean up those n objects at some later time. The only difference is you're not deallocating the array here, but that single deallocation is negligible.

From my point of view, the best way to achieve performance in this case is to not reallocate the objects at all, but to use the same ones. Add a valid bit to mark whether or not an object is valid (in use), and to reinitialize you simply set all the valid bits to false. In a similar fashion, programs don't go through and write 0's to all your memory when it's not in use. They just leave it as garbage and overwrite data as necessary.

But again, if your number of objects isn't going into the thousands, I'd say you really won't notice the performance hit.

Upvotes: 0

Mzf
Mzf

Reputation: 5260

From you question, I understand that there are not so many array's - in that case I would say, reinitialize the whole array

In cases you have a lot of work that can take 30 sec to set up maybe you do clean up instead of reinitializing everything.

If you choose to place null, you can jet some ugly exception , so I think you rather clean the object inside the array rather then set them to null

Upvotes: 0

dotNET
dotNET

Reputation: 35400

This almost entirely depends upon MyClass, specifically how many data members it contains, how much processing does its constructor (and members' constructors) require and whether it is a relatively simply operation to (re)set an object of this class to "initialized" state. A more objective answer can be obtained through benchmarking.

Upvotes: 1

Related Questions