Re: C++ Speed Vs. Java
{ Note to moderators: In the first attempt to make this post I omitted
GC collected version to keep the post small as the difference in code
is minimal. }
Keith H Duggar wrote:
The first ironically is memory hogging.
BTW, induced by the recent thread about GC, I have been experimenting a
little bit comparing memory consumption too.
I was using this simple benchmark code:
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; }
};
int main()
{
A *a[256];
for(int i = 0; i < 256; i++)
a[i] = new A(10);
for (int i = 0; i < 10000; ++i) {
delete a[i & 255];
a[i & 255] = new A(10);
}
for(int i = 0; i < 256; i++)
delete a[i];
}
and compared to this C# version:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
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(10);
for (int i = 0; i < 10000; ++i) {
a[i & 255] = new A(10);
}
}
}
}
C# run 3 times slower than C++ version (using our current allocator),
but used 30x more memory! (150MB vs 5MB)
Really excellent way how to avoid memory problems....
--
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! ]