At noon today, Teacher Li Jianzhong sent a message about the good class performance or the good struct performance (in other words, the good heap performance? Or is stack performance good ?) The topic Weibo has aroused heated discussions among cool developers including Lao Zhao. As. NET developers generally think that struct has better performance than class, Because struct remains on the stack in the application memory, while the class is stored on the managed stack, and the stack performance is higher than the stack performance. Why? I am based on the following considerations (Note: What is discussed here.. NET application memory heap and stack, not the heap and stack in the data structure ):
1) stack space in. NET is self-maintained by memory, which is cleared when used up;
2). NET requires the Garbage Collector to recycle and release the heap space. When the Garbage Collector starts the recycle job, it will pause all the threads of your program, and then find and delete the objects that are no longer used in the program based on certain algorithms. GC then organizes all the remaining objects in the heap to save space, and adjusts all the pointers related to these objects in the stack and heap. This process is very performance-consuming, obviously it will be much harder than the value type in the memory automatic management stack.
If you understand the above, you may take it for granted that the stack has better performance than the class, but the amazing thing about the cool is that you won't take it for granted, their consideration of the depth and breadth of the problem will be much broader and deeper than our average developers. Therefore, Lao Zhao asked, "When struct is passed, the entire object is copied, and the class is referenced, it seems that the copy reference cost is lower. Why do we say s performance is high? "?
Another important issue is that value types may not always be stored on the stack. value types and pointers are always placed in the declared places. For more information, click here.
In object-oriented design, when we encounter questions like struct or class, besides considering the above mentioned performance, I think we should also consider the level of abstraction of your design objects. Objects with complicated logic and high abstraction are designed as classes (structures do not support inheritance of these features ). It is reasonable to design an object that only stores values and does not copy a large number of objects into a structure.
So should I use struct or class? I 'd like to hear a Summary of the problem that Mr. Li Jianzhong finally threw out: discussing whether the class performance is good or the struct performance is good, just like discussing whether "giving birth to a child is mainly man's credit or woman's credit. Put it simply: the heap can be used for a long life cycle (shared access among several stacks and globally held), and the stack can be used for a short life cycle (only in your own stack, without copying, do not pass, do not save to set ). Therefore, heap + stack has the best performance.
If you have a better understanding and understanding of this issue, please leave a message for discussion to help programmers who want to better understand the differences between stack and stack to write higher quality code.
Postscript: when I saw the message from Zhao, I was sincere and sincere. I was afraid that I misinterpret the meaning of Daniel and misled others. Today, Zhao sent many Weibo posts to discuss this issue. I can visit Zhao's Weibo, regarding the problem of class and struct being superior or inferior, Lao Zhao's current opinion is "the conclusion of this question is that there is basically no conclusion", but when we design the program, you must select one of the class and struct. Therefore, you should check the officers and weigh them. decide who to choose.