Sign in to follow this  

[.net] ListT vs Array[] questions

This topic is 3729 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am setting up an sprite system and a game object system and I was initially thinking of just using arrays. The problem is that arrays do not change size and so I need to know in advance how many Ill need. I also know that memory allocation in game is bad, and that lists might not be the fastest to index. The advantage is that I can make a lot of lists for sorting, such as a list of sprites in use vs free ones. A list of sprites using texture page X for rendering, or a list that are sorted (maybe one list of sorted sprites by texture page). I can also add and remove from lists easily, such as moving a sprite from list 1 and add it to list 2. For the memory allocation part, I have a two part strategy. The first part is simply creating a base pool of like 1000 sprites or game objects. The second part is when I run out of free objects. If I need another sprite and the free list is empty, Ill just allocate large blocks of objects. I am not sure how long this will take but I would guess if I grow the list by 100 (or maybe even 1000) that should not cause any real noticeable hiccups in frame rate. The real question comes down to indexing of a list vs an array. If I had a list of 1000 sprites to draw, or even 10,000 such, will the performance of a for J=0... Loop be significantly worse on a list<> opposed to an array? Using an array will be a lot restrictive then using lists, but what I want to know is if the performance difference is worth the trouble?

Share this post


Link to post
Share on other sites
Quote:

I also know that memory allocation in game is bad

Poor management of memory allocation is bad, not memory allocation in general. It helps to understand the allocation model, you should read about the implementation the .NET garbage collector and the allocation methods used so you understand what ideal patterns for allocation are.

Quote:

and that lists might not be the fastest to index.

System.Collections.Generic.List<T> is a dynamic array, it has O(1) random access.

Use a List<T>, worry about changing it if it becomes a performance bottleneck later. It likely will not.

Share this post


Link to post
Share on other sites
To add to jpetrie's comments - profiling should really be a primary tool during development. Don't leave it to the end, create some repeatable test cases (you could even make it a sort of unit test) and regularly check the results. If you also architect your solution so that you can swap algorithms out then it'll be easy to make changes if and when you identify it as a bottleneck...

Also, this isn't DX specific so I'm moving it to the .NET forum for further discussion.

hth
Jack

Share this post


Link to post
Share on other sites
Quote:
Original post by TheSilverHammer
I am setting up an sprite system and a game object system and I was initially thinking of just using arrays. The problem is that arrays do not change size and so I need to know in advance how many Ill need.

I also know that memory allocation in game is bad, and that lists might not be the fastest to index. The advantage is that I can make a lot of lists for sorting, such as a list of sprites in use vs free ones. A list of sprites using texture page X for rendering, or a list that are sorted (maybe one list of sorted sprites by texture page). I can also add and remove from lists easily, such as moving a sprite from list 1 and add it to list 2.

For the memory allocation part, I have a two part strategy. The first part is simply creating a base pool of like 1000 sprites or game objects. The second part is when I run out of free objects. If I need another sprite and the free list is empty, Ill just allocate large blocks of objects. I am not sure how long this will take but I would guess if I grow the list by 100 (or maybe even 1000) that should not cause any real noticeable hiccups in frame rate.

The real question comes down to indexing of a list vs an array. If I had a list of 1000 sprites to draw, or even 10,000 such, will the performance of a for J=0... Loop be significantly worse on a list<> opposed to an array?

Using an array will be a lot restrictive then using lists, but what I want to know is if the performance difference is worth the trouble?

Well, it sounds like you've designed a profiling scenario right there. Was it quicker to write this long post to ask instead of writing a short program to see?

Share this post


Link to post
Share on other sites

This topic is 3729 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this