views:

258

answers:

3

I'm writing a plug-in for a 3D modeling program. There is a a feature of the API where you can intercept the display pipeline and insert additional geometry that will be displayed with out actually being in the model (you can see it but you can't select/move/delete etc. etc..).

Part of this feature of the API is a method that gets called on every screen refresh that is used to tell the program what extra geometry to display. Right now I have a HashSet that is iterated through with a foreach statement. OnBrep is the generic geometry class of the API.

I have an additional command that will dump the "Ghost" geometry into the actual model. I've found, that if the geometry is actually in the model the display speeds up a lot. So I'm wondering if there is a faster way to provided the list of objects to the program? Would a simple one dimensional array be significantly faster than a HashSet<>?

A: 

This here is an extensive study on the performance of hashset/dictionary/generic list

But it's about key lookups

Personnaly I think that a normal or generic list is faster for a foreach operation since it involves no indexed items/overhead (esp inserting etc should be faster).... But this is just a gut feeling.

Julian de Wit
+1  A: 

The fastest way to return a collection of objects is to return either (a) the actual physical type that was used internally to build up the collection, or (b) a type that can be cast to in such a way that data is not copied in memory. As soon as you start copying data (e.g. CopyTo, ToArray, ToList, a copy constructor, etc) you have lost time.

Having said that, unless the number of items is large, this will be a micro-optimisation and therefore probably not worth doing. In that case, just return the collection type that would be of most use to the calling code. If you are unusure, do some timing tests rather than taking a guess.

Christian Hayter
Yep this ended up being the problem. This is a Nurbs modeling program, but I found out today that it uses meshes to do the actual display. So i'm modifying my code to save a collection of meshes rather then the actual Nurbs surfaces.But it sounds like the type of collection shouldn't make a big difference in the speed?
Eric Anastas
Theoretically, no. However, in practice, the size of the data may be significant. Don't take mine or anyone else's word for it - profile your code to see if the time difference is significant or not.
Christian Hayter
A: 

Usually when working with 3D graphics, you get the best performance if you manage to reduce the draw calls/state changes as much as possible.

In your case I'd try to reduce the draw calls to a minimum by merging your adorned geometry or trying to use some sort of batching feature if it's available.

It's very likely that the frame drop is not because of using a hash list/dictionary instead of an array. (Unless there's a broken/expensive hashing function somewhere...).

Pop Catalin