What's the fastest way to get back a list of objects in C #?
I am writing a plugin for a 3D modeling program. There is an API function where you can intercept the display pipeline and insert additional geometry that will be displayed with the actual presence in the model (you can see it, but you cannot select / move / delete, etc. etc.) ..
Part of this API function is a method called every time the screen is refreshed, which is used to tell the program what additional geometry to display. Right now I have a HashSet that is iterated over with a foreach statement. OnBrep is a generic geometric API class.
I have an additional command that dumps the "Ghost" geometry into the actual model. I found that if the geometry is actually in the model, the display speeds up. So I'm wondering if there is a faster way to provide a list of objects to a program? Would a simple one-dimensional array be significantly faster than HashSet <>?
source to share
The fastest way to return a collection of objects is to return either (a) the actual physical type that was used internally to create the collection, or (b) a type that can be distinguished so that the data is not copied into memory. Once you begin to copy the data (eg, CopyTo
, ToArray
, ToList
, copy constructor, and so on), you lose time.
Having said that, if the item count is large, it would be micro-optimized and therefore probably not worth doing. In this case, just return the collection type that is most useful to the calling code. If you are unsuitable, do some timing tests, not guesswork.
source to share
This is an extensive study here about hashset / dictionary / generic list performance
But this is about key searches
Personnaly I find that a regular or generic list is faster for a foreach operation as it doesn't include indexed items / overhead (esp insertion, etc. should be faster). But it's just a gut feeling.
source to share
Usually, when working with 3D graphics, you get the best performance if you can manage to reduce the number of calls / call states as much as possible.
In your case, I'll try to keep the calls to a minimum by merging your decorated geometry or trying to use some sort of batch function if available.
It is very likely that the frame drop is not due to the use of a hashlist / dictionary instead of an array. (If there is a broken / expensive hashing function somewhere ...).
source to share