optimization - What is the downside of using a structure vs object in a list in C#? -


as understand, using structure value types give better performance using reference types in array or list. there downside involved in using struct instead of class type in generic list?

ps : aware msdn recommends struct should maximum 16 bytes, have been using 100+ byte structure without problems far. also, when maximum stack memory error exceeded using struct, run out of heap space if use class instead.

as others have pointed out, there many downsides using large structures in list. ramifications of others have said:

say you're sorting list members 100+ byte structures. every time items have swapped, following occurs:

var temp = list[i]; list[i] = list[j]; list[j] = temp; 

the amount of data copied 3*sizeof(your_struct). if you're sorting list that's made of reference types, amount of data copied 3*sizeof(intptr): 12 bytes in 32-bit runtime, or 24 bytes in 64-bit runtime. can tell experience copying large structures far more expensive indirection inherent in using reference types.

using structures reduces maximum number of items can have in list. in .net, maximum size of single data structure 2 gigabytes (minus little bit). list of structures has maximum capacity of 2^31/sizeof(your_struct). if structure 100 bytes in size, can have @ 21.5 million of them in list. if use reference types, maximum 536 million in 32-bit runtime (although you'll run out of memory before reach limit), or 268 million in 64-bit runtime. and, yes, of work many things in memory.


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -