Jump to content
  • Advertisement
Sign in to follow this  
jagguy2

collections are too slow?

This topic is 3625 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 using vb.net silverlight for game programming. I have been using an array of structure for my program to make a game and this is what i want to happen. I dont know if collections are slower than arrays in .net . I want to create an array of objects and when i set a property to true I want the object to be added to a collection . THis way instead of searching the whole object array to see if a property is true I can use this extra collection of relevant objects. The problem is that i am creating an extra collection in global memory. Is this a good idea? I am fed up with having so many for loops in my code searching for objects who have a property set to false.

Share this post


Link to post
Share on other sites
Advertisement
You can use LINQ to select only the objects that match certain criteria in order to avoid the for loops, but to answer your question... no, an extra collection will not introduce any kind of significant speed penalty here. Disregarding the fact that these things do not have inherent speed, containers only incur minimal overhead over regular arrays, and having an extra one won't noticably impact performance.

Share this post


Link to post
Share on other sites
why am i reading it does and i thought it would because if you add,insert and delete objects in a collection this requires extra processing that you dont have to do. Surely this would incur a time penalty with its functionality?

it is good news if it is ok.

Also i have a background of tiles that will need to have about 1000 objects in a collection and images. So i will find out soon enough i guess.

Share this post


Link to post
Share on other sites
Some collections will have slower insertions and deletions than other collections and arrays.

This is usually done so that you get faster performance in other areas. For example, Dictionarys have a slower insertion time than a linked list, but searching for an element based on a key is much faster.

You need to pick a collection by matching up its advantages to your usage.

Share this post


Link to post
Share on other sites
"Collections" are not too slow. A specific type of collection may be 'too slow' for a specific requirement, just as arrays may be 'too slow' for data that inherently has a non-array like structure. If you intelligently pick the correct container type for your problem, the [arguably nonexistent] performance differences will be amortized by the time you would save in implementation, that you could focus on optimizing something that would actually benefit from your efforts.

If you're looking for something really close to an array, you can use what is effectively a dynamically resizing array by using List<T>. You don't get any fancy effects, but it is nearly functionally equivalent to an array, with some extra sugar sprinkled on top by allowing you resizing and access to some neat functionality.

In all likelihood though, if an array is not acceptable, it is likely because your data doesn't lend itself well to being crammed into an array. In this case, you should pick a different collection that provides functionality more in line with your interests.

I use collections all the time, and am pretty sure that I would have noticed a 10x performance hit.

[Edited by - Drigovas on July 20, 2008 8:43:41 AM]

Share this post


Link to post
Share on other sites
good i much prefer collections as you can add and remove elements.

also to search through is for each quicker than using a for loop or doesnt it matter?

Share this post


Link to post
Share on other sites
The performance is likely equivalent in most cases -- but perhaps not. I would recommend preferring foreach (unless you can't, like if you need to modify the collection during iteration) since it tends to be more idiomatic. But there are contexts in which for is more appropriate (when modification is desired as above, when you need the index for some other context, et cetera). Choose the method that is most appropriate for correctness; always favor correctness over performance. Only a small percentage -- 15% or so as a crude statistic -- of your code is ever actually a bottleneck, you don't want to be worried about performance concerns at such a small scale until you have determined that there is a bottleneck in that code.

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!