473,386 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

I'm Shocked

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[i]=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,star tingtime.wMinute,startingtime.wSecond,startingtime .wMilliseconds);printf("%2d:%2d:%2d:%2d\n",endtime .wHour,endtime.wMinute,endtime.wSecond,.wMilliseco nds); 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

Mar 3 '07 #1
11 1936
Bas
Hi people,

I saw it.. not the correct newsgroup. I'm sorry!

Bas

"Bas" <da**********@jaja.nlschreef in bericht
news:45**********************@news.kpnplanet.nl...
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[i]=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,star tingtime.wMinute,startingtime.wSecond,startingtime .wMilliseconds);printf("%2d:%2d:%2d:%2d\n",endtime .wHour,endtime.wMinute,endtime.wSecond,.wMilliseco nds);
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

Mar 3 '07 #2
* 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[i]=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,star tingtime.wMinute,startingtime.wSecond,startingtime .wMilliseconds);printf("%2d:%2d:%2d:%2d\n",endtime .wHour,endtime.wMinute,endtime.wSecond,.wMilliseco nds); 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<Testv( 30000000 );
}

?
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 3 '07 #3
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" <al***@start.noschreef in bericht
news:54*************@mid.individual.net...
>* 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[i]=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,sta rtingtime.wMinute,startingtime.wSecond,startingtim e.wMilliseconds);printf("%2d:%2d:%2d:%2d\n",endtim e.wHour,endtime.wMinute,endtime.wSecond,.wMillisec onds);
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<Testv( 30000000 );
}

?
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Mar 3 '07 #4
Bas wrote:
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
Mar 3 '07 #5
John Harrison wrote:
Bas wrote:
>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.
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
Mar 3 '07 #6

"Bas" <da**********@jaja.nlwrote in message
news:45***********************@news.kpnplanet.nl.. .
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?

Mar 3 '07 #7
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

"John Harrison" <jo*************@hotmail.comschreef in bericht
news:Xk*****************@newsfe2-win.ntli.net...
Bas wrote:
>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

Mar 3 '07 #8
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

"Chris Theis" <chris.theis@n o s p am.cern.chschreef in bericht
news:es**********@cernne03.cern.ch...
>
"Bas" <da**********@jaja.nlwrote in message
news:45***********************@news.kpnplanet.nl.. .
>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?

Mar 3 '07 #9
On Sat, 3 Mar 2007 12:32:26 +0100 in comp.lang.c++, "Bas"
<da**********@jaja.nlwrote,
>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.

Mar 3 '07 #10
On Mar 3, 11:24 pm, "Alf P. Steinbach" <a...@start.nowrote:
Bas wrote:

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

std::vector<Testv( 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).

Mar 4 '07 #11
* Old Wolf:
On Mar 3, 11:24 pm, "Alf P. Steinbach" <a...@start.nowrote:
>Bas wrote:
>> Test *ar[30000000];
for(long int j = 0; j< 30000000; j++)
ar[j] = new Test;
std::vector<Testv( 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

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 4 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Rob Panosh | last post by:
I posted the message below last Friday and nobody replied, once again it would be greatly appreciated if somebody could elaborate on the message "The thread '<No Name>' (0x94c) has exited with code...
4
by: Bas | last post by:
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.