I am writing an application where performance is quite important.
I need to store templates in memory. A template is an array of integers.
I was trying to use recordsets, but they seem pretty expensive to use and I've had great performance increases from using arrays so I decided to try that.
Each Template has an ID which is an integer (0 - 65535)
I created a structure for a template and created an array whose data type is that structure.
Public Structure udtTemplateRecord
Dim TemplateType As UShort
Dim Count As UShort
Dim Count2 As UShort
Dim Type() As UShort
Dim Length() As UShort
End Structure
Dim Array(65535) as udtTemplateRecord
I was going to do this to look up a template (the lookup is where performance is important)
Dim Template as udtTemplateRecord
Template = Array(TemplateID)
While lookups should be pretty much instant as I am simply referencing an array by index, I realised that this will start to consume a lot of memory, especially when I start to have many of these structures in memory.
Also it is inefficient as there will probably never be more than a 50-200 templates in memory at any one time (often as few as 3 or 4), but the array would be large (65535).
Then I saw mention of a hashtable and I read this as only storing a template when I need it, rather than allocating space for every possible template ID but being optimised for lookups. Some documentation pointed out that a dictionary object is newer/better. Then I came across Hash Sets and lists.
I have never had to work on in-memory data like this before and usually performance of this part of a program is not important. However it is this time.
To be fair, I haven't a clue about pro's and cons of the above and don't want to spend a day or two reading about them all and trying to get code to work.
I'd love a brief note on which structure (hash set, hashtable, dictionary, list etc) is best to use (with lookup performance being key). I don't need to iterate through the data, sort it, filter it etc, simply look a template by it's ID (Integer) and return a structure .
A bit of code to get me going would be super useful.
Thanks in advance
John
Our community of experts have been thoroughly vetted for their expertise and industry experience.