I have a class with several static arrays:
int [] with 17,720 elements
string [] with 17,720 elements
I noticed that when I first access this class, it takes almost 2 seconds to initialize, which causes a pause in the graphical interface that accesses it.
In particular, it is a search for Unicode character names. The first array is the index into the second array.
static readonly int[] NAME_INDEX = {
0x0000, 0x0001, 0x0005, 0x002C, 0x003B, ...
static readonly string[] NAMES = {
"Exclamation Mark", "Digit Three", "Semicolon", "Question Mark", ...
The following code is how arrays are used (given the character code). [Note: this code is not a performance issue]
int nameIndex = Array.BinarySearch<int>(NAME_INDEX, code);
if (nameIndex > 0) { return NAMES[nameIndex]; }
I think that I am looking at other options for structuring data so that 1) the class loads quickly and 2) I can quickly get the "name" for the given character code.
Should I store all these thousands of elements in static arrays?
Update
Thanks for all the suggestions. I tested the Dictionary approach, and the performance of adding all entries seems very poor.
Below is the code with Unicode data for testing arrays and dictionaries http://drop.io/fontspace/asset/fontspace-unicodesupport-zip
Solution Update
I tested my original double arrays (which are faster than both versions), with a background thread for initialization, which helped performance a bit.
However, the real surprise is how well binary files work in resource streams. This is the fastest solution discussed in this thread. Thank you all for your answers!