I am 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
 
B

Bronek Kozicki

Bas said:
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

you just tested performance of heap allocator in particular
implementation of these languages. It has very little to do with the
programming language and much more to its runtime environment. Eg. in
..NET objects allocated on heap are allowed to be moved around in memory
which gives extra freedom to optimization. If you feel like testing, you
might test C++ heap implementation that is not merely a C++ abstraction
over operating system heap, eg. www.microquill.com


B.
 
J

Jochen Kalmbach [MVP]

Hi Bas!
so I had to try the speed of both: allocate 30,000,000 objects of a class,
and clocking the time necessary.

Allocation in C# (.NET) is only an "increment" operation.
In (native) C++ it is "walking a linked-list"...

Therefor allocation in C# is the fastest possible solution.

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
A

Arnaud Debaene

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.

Sorry to say it, but this kind of performance tests is totally irrelevant,
because it checks only one aspect of the environment (ie, heap allocation
speed), which doesn't give you a clue about how will react a true, complete
program : In your code, you're for example not tesing neither object
destruction/finalization, neither heap deallocation.
Here is the code
<snip>

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!?!?

This is not surprising : The garbage collector in .NET makes it sure the
heap is (mainly) compacted all the time. Therefore, allocating some memory
in .NET is merely adjusting the pointer to the top of the compacted heap.
On the other hand, in native C++, the heap manager typically maintains a
linked list of free-blocks (memory chunks that can be allocated). Finding a
free block therefore involves a linear linked-list search.

There is also another point to your test : Since yo allocate a bunch of
memory, you pus a lot of pressure on the available RAM. You need to make
those tests several time and check that page swapping doesn't kick-in during
your test-run and fool your results.

Arnaud
MVP - VC
 

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