I am Shocked



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,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

.



Relevant Pages

  • Re: I am Shocked
    ... I cannot resist sometimes to compare these two languages (as far as I can, ... private double d; ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Getting and Setting and best practise
    ... to interpret which private variable you wanted. ... C - you make seperate getter/setter functions for each private ... if I've been a good little programmer ... interface to the object INSIDE of the object. ...
    (comp.lang.php)
  • Re: A question (confusion) about closure
    ... Local variables have dynamic duration and local accessibility. ... Own/static variables have indefinite duration and local (private) accessibility. ... Closures are just like own/static variables, ... system and each programmer sets up his/her own private sub-domains ...
    (comp.lang.lisp)
  • Re: fields or properties
    ... All you have to do is change the name of the (now private) field, ... manipulated _only_ within the owning class, ... one class and find _all places_ where that field is changed, and modify ... I'm a lazy programmer and I don't like cluttering my brain ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Help with error
    ... * a copy constructor ... This class needs a destructor for deleting the allocated memory. ... can be declared private and be not implemented. ...
    (alt.comp.lang.learn.c-cpp)

Loading