c# - Dictionary of large objects vs. Dictionary of array indices -


i have large objects want access via string identifier. current approach use dictionary containing those:

var myobjects = new dictionary<string, largeobjectclass>(); var specificobject = myobjects["identifier"]; 

now wondering whether storing many of large objects in dictionary might bad performance , better off using dictionary store indices array stores objects:

var myobjects = new largeobjectclass[size]; var objectindices = new dictionary<string, int>(); var specificobject = myobjects[objectindices["identifier"]]; 

this bad approach if size of myobjects unknown in advance or might change @ runtime, since dictionary smaller , read somewhere arrays more efficient dictionaries, thought approach might have better performance in cases size fixed.

which of these approaches more efficient, assuming objects large?

you're better off using dictionary<> in case. remember both dictionary , array storing references large objects because class instances reference types. dictionary smaller if it's storing ints storing objects. small difference overshadowed fact array would, itself, storing object references, combined total take more space dictionary would.

an array give better performance if allow avoid using dictionary @ all. might happen, example, if keying objects based on consecutive int values rather strings. adding array on top of dictionary going worse in every way.

also, general rule, should use simplest, maintainable approach until have performance problem. dictionary<> highly unlikely cause performance problems unless you're invoking millions of times.


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -