I'm Shocked


B

Bas

Hello,

Until 6 years ago I was a C++ programmer. The last 6 years I've done
something else than computerprogramming, but now I'm a bit back, learning
myself C#.
I cannot resist sometimes to compare these two languages (as far as I can,
not programming for 6 years is quitte a time),
so I had to try the speed of both: allocate 30,000,000 objects of a class,
and clocking the time necessary.
Here is the code for C#:

namespace Temp
{
class Program
{
static void Main(string[] args)
{
DateTime t1 = DateTime.Now;
Test[] tab = new Test[30000000];
for (long i = 0; i< 30000000; i++) {
tab=new Test();
}
DateTime t2 = DateTime.Now;
Console.Write("Begin time is {0}:{1}:{2}:{3}\n", t1.Hour, t1.Minute,
t1.Second, t1.Millisecond);
Console.Write("End time is {0}:{1}:{2}:{3}\n", t2.Hour, t2.Minute,
t2.Second, t2.Millisecond);
Console.Read();
}
}

class Test{
private int i;
private double d;
private float x;

public Test(){
i=123;
d=3.1415926;
x=6.28f;
}
}
}



and here it is for C++:

#include "stdafx.h"
#include <windows.h>

class Test {
private:
int i;
double d;
float fl;

public:
Test() {i = 123; d = 3.1415926; fl = 6.28f; }
};

int _tmain(int argc, _TCHAR* argv[])
{
SYSTEMTIME startingtime, endtime;

GetLocalTime(&startingtime);

Test *ar[30000000];
for(long int j = 0; j< 30000000; j++)
ar[j] = new Test;

GetLocalTime(&endtime);

printf("%2d:%2d:%2d:%2d\n",startingtime.wHour,startingtime.wMinute,startingtime.wSecond,startingtime.wMilliseconds);printf("%2d:%2d:%2d:%2d\n",endtime.wHour,endtime.wMinute,endtime.wSecond,.wMilliseconds); getchar(); return 0;}I hope someone will say "YOU MORON..!"and that I've done something wrong..But the C# program was ready within 11seconds.. the C++ program took 15seconds; so it was SLOWER.How can thisbe!?!?Kind regards,Bas from Holland
 
Ad

Advertisements

A

Alf P. Steinbach

* Bas:
Hello,

Until 6 years ago I was a C++ programmer. The last 6 years I've done
something else than computerprogramming, but now I'm a bit back, learning
myself C#.
I cannot resist sometimes to compare these two languages (as far as I can,
not programming for 6 years is quitte a time),
so I had to try the speed of both: allocate 30,000,000 objects of a class,
and clocking the time necessary.
Here is the code for C#:

namespace Temp
{
class Program
{
static void Main(string[] args)
{
DateTime t1 = DateTime.Now;
Test[] tab = new Test[30000000];
for (long i = 0; i< 30000000; i++) {
tab=new Test();
}
DateTime t2 = DateTime.Now;
Console.Write("Begin time is {0}:{1}:{2}:{3}\n", t1.Hour, t1.Minute,
t1.Second, t1.Millisecond);
Console.Write("End time is {0}:{1}:{2}:{3}\n", t2.Hour, t2.Minute,
t2.Second, t2.Millisecond);
Console.Read();
}
}

class Test{
private int i;
private double d;
private float x;

public Test(){
i=123;
d=3.1415926;
x=6.28f;
}
}
}



and here it is for C++:

#include "stdafx.h"
#include <windows.h>

class Test {
private:
int i;
double d;
float fl;

public:
Test() {i = 123; d = 3.1415926; fl = 6.28f; }
};

int _tmain(int argc, _TCHAR* argv[])
{
SYSTEMTIME startingtime, endtime;

GetLocalTime(&startingtime);

Test *ar[30000000];
for(long int j = 0; j< 30000000; j++)
ar[j] = new Test;

GetLocalTime(&endtime);

printf("%2d:%2d:%2d:%2d\n",startingtime.wHour,startingtime.wMinute,startingtime.wSecond,startingtime.wMilliseconds);printf("%2d:%2d:%2d:%2d\n",endtime.wHour,endtime.wMinute,endtime.wSecond,.wMilliseconds); getchar(); return 0;}I hope someone will say "YOU MORON..!"and that I've done something wrong..But the C# program was ready within 11seconds.. the C++ program took 15seconds; so it was SLOWER.How can thisbe!?!?Kind regards,Bas from Holland


Doesn't look much like standard C++ to me.

Anyway, meaningless without knowing machine, OS, compiler, compiler
options etc.

Did you try

#include <vector>

class Test
{
private:
int i;
double d;
float fl;

public:
Test() {i = 123; d = 3.1415926; fl = 6.28f; }
};

int main()
{
std::vector<Test> v( 30000000 );
}

?
 
B

Bas

Alf,
thanks for the answer.

Both run on the same machine, Windows XP, Microsoft compilers. I've chosen
for speed optimization in both cases. I know its difficult to compare, but
the difference is significant.
I found an answer, but still..It seems that the .NET runtime environment has
a better heap management. The garbage collector keeps much better track of
free space. (I read that in Professional C# 2005). Still the difference
surprises me a lot. I can imagine it's true for a program that creates and
destroys at random a lot of instances, but in my examples the heap of both
programs should be fresh and clean, I think, at the beginning. It has just
to add a lot of instances, nothing more.


Alf P. Steinbach said:
* Bas:
Hello,

Until 6 years ago I was a C++ programmer. The last 6 years I've done
something else than computerprogramming, but now I'm a bit back, learning
myself C#.
I cannot resist sometimes to compare these two languages (as far as I
can,
not programming for 6 years is quitte a time),
so I had to try the speed of both: allocate 30,000,000 objects of a
class,
and clocking the time necessary.
Here is the code for C#:

namespace Temp
{
class Program
{
static void Main(string[] args)
{
DateTime t1 = DateTime.Now;
Test[] tab = new Test[30000000];
for (long i = 0; i< 30000000; i++) {
tab=new Test();
}
DateTime t2 = DateTime.Now;
Console.Write("Begin time is {0}:{1}:{2}:{3}\n", t1.Hour,
t1.Minute,
t1.Second, t1.Millisecond);
Console.Write("End time is {0}:{1}:{2}:{3}\n", t2.Hour, t2.Minute,
t2.Second, t2.Millisecond);
Console.Read();
}
}

class Test{
private int i;
private double d;
private float x;

public Test(){
i=123;
d=3.1415926;
x=6.28f;
}
}
}



and here it is for C++:

#include "stdafx.h"
#include <windows.h>

class Test {
private:
int i;
double d;
float fl;

public:
Test() {i = 123; d = 3.1415926; fl = 6.28f; }
};

int _tmain(int argc, _TCHAR* argv[])
{
SYSTEMTIME startingtime, endtime;

GetLocalTime(&startingtime);

Test *ar[30000000];
for(long int j = 0; j< 30000000; j++)
ar[j] = new Test;

GetLocalTime(&endtime);


printf("%2d:%2d:%2d:%2d\n",startingtime.wHour,startingtime.wMinute,startingtime.wSecond,startingtime.wMilliseconds);printf("%2d:%2d:%2d:%2d\n",endtime.wHour,endtime.wMinute,endtime.wSecond,.wMilliseconds);
getchar(); return 0;}



I hope someone will say "YOU MORON..!"and that I've done something
wrong..But the C# program was ready within 11seconds.. the C++ program took
15seconds; so it was SLOWER.How can thisbe!?!?Kind regards,Bas from Holland
 
J

John Harrison

Bas said:
Alf,
thanks for the answer.

Both run on the same machine, Windows XP, Microsoft compilers. I've chosen
for speed optimization in both cases. I know its difficult to compare, but
the difference is significant.
I found an answer, but still..It seems that the .NET runtime environment has
a better heap management. The garbage collector keeps much better track of
free space. (I read that in Professional C# 2005). Still the difference
surprises me a lot. I can imagine it's true for a program that creates and
destroys at random a lot of instances, but in my examples the heap of both
programs should be fresh and clean, I think, at the beginning. It has just
to add a lot of instances, nothing more.

In other words what you have found is a quality of implementation issue
not a language issue.

There's no reason the C++ heap management couldn't be improved to match
the C# one. Nor is there any reason not to think that the C++ heap
wouldn't be better on a different test.

But (to bring this back to language issues) in C++ you can write your
own heap management routines which will slot into a C++ program with no
changes needed to the rest of the code. I'm not sure if C# can do that
(correct me if I'm wrong).

You might like to try your example with the 'small object allocator' by
Alexei Alexandrescu (you can find it in the book Modern C++ Design).
When I tried a similar example to your I got a 30% speed improvement
over the generic C++ allocator. I was also shocked.

john
 
B

Bo Persson

John said:
In other words what you have found is a quality of implementation
issue not a language issue.

There's no reason the C++ heap management couldn't be improved to
match the C# one. Nor is there any reason not to think that the C++
heap wouldn't be better on a different test.

There is always an advantage for garbage collected systems, that they can be
much faster in allocating memory, if they postpone the administative work
till collection time. In this test, the collector never runs.


Bo Persson
 
Ad

Advertisements

C

Chris Theis

Bas said:
Alf,
thanks for the answer.

Both run on the same machine, Windows XP, Microsoft compilers. I've chosen
for speed optimization in both cases. I know its difficult to compare, but
the difference is significant.
I found an answer, but still..It seems that the .NET runtime environment
has a better heap management. The garbage collector keeps much better
track of free space. (I read that in Professional C# 2005). Still the
difference surprises me a lot. I can imagine it's true for a program that
creates and destroys at random a lot of instances, but in my examples the
heap of both programs should be fresh and clean, I think, at the
beginning. It has just
to add a lot of instances, nothing more.

The comparison you're doing here is somewhat difficult to interpret & judge
because the results are mainly compiler, OS + runtime environment dependent.

In your test program you're mainly seeing the results of the heap management
or mis-management of the RTE and this in turn is very OS specific. For
example in the German magazine c't 04/2007 there was a comparison of the
SPEC2006/Xalancbmk benchmark using the same compiler version on two
different versions of their operating systems. What they found where
differences in the runtime of e.g. 1800s vs. 8680s! They also tried the
latest compiler version getting 380s vs. 7390s.

As you can see the same code might give you significantly varying
performance depending on the compiler version and especially on the OS.
Using dedicated heap libs might give you an even greater performance boost,
while you do not touch your code at all.
Thus, heap allocation intensive tests are rather suited to compare OS
heap-management and compilers.

Cheers
Chris

P.S.: Have you tried for example setting the /heap parameter?
 
B

Bas

John,
thanks a lot. I've gotten this (similar) answer from ohter people too. It
seems not a language issue but an runtime environment issue... pfew ..!

Bas
 
B

Bas

Hi,

No I have not tried the /heap parm.
But your answer is the same as most responses: it seems not a language issue
but merely an operating system issue; the way the runtime environment
handles memory.
Thanks a lot for all your answers!

Bas
 
D

David Harmon

On Sat, 3 Mar 2007 12:32:26 +0100 in comp.lang.c++, "Bas"
I found an answer, but still..It seems that the .NET runtime environment has
a better heap management. The garbage collector keeps much better track of
free space.

Benchmarking is fraught with difficulty. I don't know if the garbage
collector even had to run during your test. Certainly any uncollected
garbage at the end could remain uncollected.
 
O

Old Wolf

Bas said:
Test *ar[30000000];
for(long int j = 0; j< 30000000; j++)
ar[j] = new Test;

std::vector<Test> v( 30000000 );

The thing being benchmarked was 30000000 small allocations.
Your suggested code performs 1 large allocation.

(I'm sure you know this; but just clearing it up for other readers who
may not).
 
Ad

Advertisements

A

Alf P. Steinbach

* Old Wolf:
Bas said:
Test *ar[30000000];
for(long int j = 0; j< 30000000; j++)
ar[j] = new Test;
std::vector<Test> v( 30000000 );

The thing being benchmarked was 30000000 small allocations.
Your suggested code performs 1 large allocation.

(I'm sure you know this; but just clearing it up for other readers who
may not).

Yes. The point is that for a an ordinary C# 'class' object (reference
semantics) you have to do those 30M separate allocations to have the 30M
objects you need, whatever the reason could be for that need, whereas in
C++ you can do just one big allocation. So as a /language/ comparision
this one decidedly favors C++, some millions times faster than C#.

I think if the comparision is meaningful in any way, then that way must
be it.

Cheers,

- Alf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top