473,395 Members | 1,670 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,395 software developers and data experts.

I am 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
4 1239
Bas wrote:
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.
Mar 3 '07 #2
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/
Mar 3 '07 #3
Did you compare release builds?
--
Vladimir Nesterovsky
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 #4

"Bas" <da**********@jaja.nla écrit dans le message de news:
45**********************@news.kpnplanet.nl...
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
Mar 4 '07 #5

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...
11
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.