Jump to content
  • Advertisement
Sign in to follow this  
Kitasia

ToArray() Efficiency

This topic is 3654 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 have a List<> of points creating a rail and when I draw the rail I convert the list to an array. I'm not sure how fast that works.I was wondering should I leave it the same, create an array inside a rail class that is updated whenever the points change, or turn the array of points into a list whenever I need to edit the array then change it back? If I had to guess, I'd say you'd all recommend the latter depending on how often I remove and add points which isn't often at all : ). It's nice to know for sure though.

Share this post


Link to post
Share on other sites
Advertisement
Ah forgot I was posting on a general board.

C#, Visual Studio 2008 and I believe lists would be under System.Collections.Generic

Share this post


Link to post
Share on other sites
Why do you need to convert the list to an array in the first place? The choice of container rather depends on its ultimate usage.

Share this post


Link to post
Share on other sites
ToArray creates a /copy/ of the array. It is an O(n) operation (read slow).

The high-performant answer to this question is use a b-tree.
You should start by allocating an array that has enough space in it to handle most cases. When you want to change the data you have a heuristic (controlled by the b-tree impl) that shifts the items in a small until it runs out of room. Then it start breaking everything down into an n-tree of array 'chunks'.

To process/render/etc... you have to iterate the b-tree and execute on each array chunk. To edit the array you only have to copy the configured max chunk size.

It balances the desire for contiguous chunks of RAM for fast processing with the desire for a linked-list for fast edits.


To keep it simple, just use an array and keep track of how much of the array is used. That way you don't have to keep calling ToArray().

Share this post


Link to post
Share on other sites
Quote:

I have a List<> of points creating a rail and when I draw the rail I convert the list to an array. I'm not sure how fast that works.I was wondering should I leave it the same, create an array inside a rail class that is updated whenever the points change, or turn the array of points into a list whenever I need to edit the array then change it back?


List<T> isn't a linked list, it's a dynamic array. I'm not seeing a reason for the conversion to array and back all the time. Why not just leave it as a List<T>?

Share this post


Link to post
Share on other sites
I'd like to use the functions of lists. The built in functions for arrays are rather slim. The main reason for the conversion is drawing things using DirectX/XNA requires the use of arrays.

If I store the data as an array and only convert to a list when I edit the size of the array I can avoid reinventing the wheel inside of the class. After the addition I convert the list back into an array. To my understanding the operation seems relatively painless.

From what I've heard thus far, ToArray() isn't harmless enough to be done every frame?

Share this post


Link to post
Share on other sites
Quote:
Original post by AntiGuy
I'd like to use the functions of lists. The built in functions for arrays are rather slim.


Which functionality?

Quote:
The main reason for the conversion is drawing things using DirectX/XNA requires the use of arrays.


Are you sure about that? How are you doing the drawing at the moment?

Quote:
From what I've heard thus far, ToArray() isn't harmless enough to be done every frame?


Well, is it working right now? Are you looking for a performance problem and suspecting this might be it?

Share this post


Link to post
Share on other sites
If you don't mind some evil, you can use reflection to get a hold of the internal array that List uses, instead of copying one off. But you didn't hear that from me.

Share this post


Link to post
Share on other sites
Quote:
Which functionality?

I mainly like lists for the ability to quickly add and remove things. The rest is just a bonus.

Quote:
Are you sure about that? How are you doing the drawing at the moment?
I'm quite sure, if you've seen the DrawUserPrimitives method it calls for an array of vertex data.

Quote:
Well, is it working right now? Are you looking for a performance problem and suspecting this might be it?


Sure things are working fine, however it's nice to be aware of bottlenecks in advance instead of realizing you need to reconstruct your code because of one silly method.

Quote:
If you don't mind some evil, you can use reflection to get a hold of the internal array that List uses, instead of copying one off. But you didn't hear that from me.


I love the sound of that : )!! But what makes it evil? Is it susceptible to error in some way or still slows things down...?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!