Why does reusing arrays increase performance so significantly in c#? -
In my code, I work in a large number, to store data on a temporary basis, Array is required. I have around 500 tasks at the beginning of every work, I allocate memory for an array:
double [] tempDoubleArray = new double [M];
M is a large number based on precise work, usually around 2000000. Now, I do some complex calculations to fill the array, and in the end I use the array to determine the result after that, after this work, tempDoubleArray goes out of the scope.
Profiling indicates that the call to create arrays is time consuming, so, I decide to try and reuse the array, by making it stable and using it again. To find the minimum size of this array, a few additional jogging is required, in which all tasks require extra pass, but it works. Now, the program is very fast (for the execution of all tasks from 80 seconds to 22 seconds).
double [] tempDoubleArray = staticDoubleArray;
However, why do I have a little bit in the dark why it works right. The ID says that in the original code, when tempDoubleArray exits scope, then it can be collected, so it should not be right to allocate a new array?
I ask this because it understands why it works because I understand other ways of achieving the same effect, and because I should know how the performance problems in the allocation of cases Are there.
Just because may be does not mean that it will be . In fact, the garbage collectors in your collection were as aggressive, your performance would be very bad.
Keep in mind that creating a single array does not create just one variable, being the number of elements in the N
variable ( N
variable) ) Reusable arrays are a great way to increase your performance, though you have to be careful.
To clarify, what is the meaning of "creating variables", especially allocating a place for them and to make the runtime runable for them to be usable (i.e. zero / null To start the values). Because arrays are reference types, they accumulate on a heap, which makes life a bit more complex in terms of memory allocation. Based on the size of the array (whether it is more than 85kb in the total storage space or not), it will be stored in a common pile or large object heap. An array stored on the common heap can trigger a combination of garbage collection and stack, along with all other heap objects (which includes currently walking around the usage-memory to maximize the available location) large object heap An orgate stored on it will not trigger the compaction (as LOH is never compact), but before taking the second largest closest block of memory The summer may be collected at time.
Comments
Post a Comment