|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Is new & free in CLR heap fast?I am a newbie in C# programming. I've been working in C++ language for some
years. The new/delete statement in C++ is rather slow so it is recommended using static data structure in some cases which require very fast performance as you know. However, there's no way but using value type class in C# for avoiding heap access as far as I know. I think C# is very slow unless it has something which enables CLR very fast in accessing CLR heap memory. The best way is to just test the new/delete performance of C# by myself, however, it is very difficult testing every case I can imagine. Please reply. Thanks in advance. Hyun-jik Bae Hyun-jik Bae wrote:
> I am a newbie in C# programming. I've been working in C++ language First, you must realize that the CLR uses a generational heap. In the > for some years. > The new/delete statement in C++ is rather slow so it is recommended > using static data structure in some cases which require very fast > performance as you know. However, there's no way but using value type > class in C# for avoiding heap access as far as I know. I think C# is > very slow unless it has something which enables CLR very fast in > accessing CLR heap memory. > The best way is to just test the new/delete performance of C# by > myself, however, it is very difficult testing every case I can > imagine. Please reply. Thanks in advance. current implementation, the heap is divided into 3 generations, called Gen0, Gen1 and Gen2 (there's also a Large Block heap, but it doesn't enter into the picture unless you're allocating large objects, in which case other considerations generally swamp the time required to allocate). Allocations are made from the Gen0 heap and promoted to Gen1 and Gen2 if the object stays active long enough. Allocation from the Gen0 heap is about 2 instructions (seriously). The Gen0 heap is a simple mark/release heap for which allocation simply involves subtracting the allocation size from the marker and returning the resulting pointer. Free is accomplished by moving all live Gen0 objects into the Gen1 heap and then completely freeing the Gen0 heap by moving the marker back to the top. Gen1 and Gen2 heaps are closer to C++ heap in that they have relatively expensive allocators and deallocators. On top of that, objects in the Gen1 and Gen2 heaps can be moved by the GC during heap compaction. Generally, short-lived objects never leave the Gen0 heap. Since over 90% of the objects used in a typical program are short-lived, and the performance of the Gen0 heap is comparable to that of stack-based allocation, the CLR can actually give better performance than an optimized C++ program in some situations. On average, it's within about 15% of the same performance. -cd |
|||||||||||||||||||||||