Re: Garbage Collection - The Trash Begins To Pile Up
{ Note: this article contains performance measurements that may be in
breach of Microsoft's EULA for .NET, said EULA restriction included with
the .NET SDK, with Windows Vista, and IIRC even with Microsoft
MediaPlayer for any Windows. However, it's not stated in the article
that this was measured under .NET. Since we haven't discussed this I
can't speak for the moderators as a group, but I feel fairly certain
that we will not reject a posting even /if/ it's clearly in breach of
some EULA that forbids measurements to be published. -mod/aps }
Dave Harris wrote:
GC is faster to execute and take less memory than explicit heap management
OK, as I said, you got me interested, so I tried to put it to test.
C# version:
class A {
private A a;
private A b;
public A(int n) {
if(n > 0)
{
a = new A(n - 1);
b = new A(n - 1);
}
}
}
class Program {
static void Main(string[] args)
{
A[] a = new A[256];
for(int i = 0; i < 256; i++)
a[i] = new A(4);
for (int i = 0; i < 10000000; ++i) {
a[i & 255] = new A(4);
}
}
}
}
C++ version:
class A {
A *a;
A *b;
public:
A(int n) {
if(n) {
a = new A(n - 1);
b = new A(n - 1);
}
else
a = b = NULL;
}
~A() {
delete a;
delete b;
}
};
CONSOLE_APP_MAIN
{
A *a[256];
for(int i = 0; i < 256; i++)
a[i] = new A(4);
for (int i = 0; i < 10000000; ++i) {
delete a[i & 255];
a[i & 255] = new A(4);
}
for(int i = 0; i < 256; i++)
delete a[i];
}
(U++ heap allocator was used)
Results:
C#: real:10.671 user:10.265 sys: 0.062 page:10292 KB work:7284 KB
C++: real:10.625 user:10.562 sys: 0.015 page: 592 KB work:1536 KB
OK, this was the first (and hopefully the last) time I have ever used
C#, it is possible I have done something wrong.
If not, I officially proclaim GC speed & size assertions a *MYTH*.
(Especially *size* ;)
--
Mirek Fidler
U++ team leader. http://www.ultimatepp.org
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]