I'm Shocked

Discussion in 'C++' started by Bas, Mar 3, 2007.

  1. Bas

    Bas Guest

    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
     
    Bas, Mar 3, 2007
    #1
    1. Advertising

  2. Bas

    Bas Guest

    Hi people,

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

    Bas

    "Bas" <> schreef in bericht
    news:45e94946$0$2019$...
    > 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
    >
     
    Bas, Mar 3, 2007
    #2
    1. Advertising

  3. * 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 );
    }

    ?


    --
    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?
     
    Alf P. Steinbach, Mar 3, 2007
    #3
  4. Bas

    Bas Guest

    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" <> schreef in bericht
    news:...
    >* 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 );
    > }
    >
    > ?
    >
    >
    > --
    > 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?
     
    Bas, Mar 3, 2007
    #4
  5. 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
     
    John Harrison, Mar 3, 2007
    #5
  6. Bas

    Bo Persson Guest

    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
     
    Bo Persson, Mar 3, 2007
    #6
  7. Bas

    Chris Theis Guest

    [OT] Re: I'm Shocked

    "Bas" <> wrote in message
    news:45e95ca8$0$16884$...
    > 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?
     
    Chris Theis, Mar 3, 2007
    #7
  8. Bas

    Bas Guest

    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" <> schreef in bericht
    news:XkdGh.6299$...
    > 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
     
    Bas, Mar 3, 2007
    #8
  9. Bas

    Bas Guest

    Re: [OT] Re: I'm Shocked

    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.ch> schreef in bericht
    news:esbpgu$iem$...
    >
    > "Bas" <> wrote in message
    > news:45e95ca8$0$16884$...
    >> 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?
     
    Bas, Mar 3, 2007
    #9
  10. Bas

    David Harmon Guest

    On Sat, 3 Mar 2007 12:32:26 +0100 in comp.lang.c++, "Bas"
    <> wrote,
    >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.
     
    David Harmon, Mar 3, 2007
    #10
  11. Bas

    Old Wolf Guest

    On Mar 3, 11:24 pm, "Alf P. Steinbach" <> wrote:
    > Bas wrote:
    > >
    > > 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).
     
    Old Wolf, Mar 4, 2007
    #11
  12. * Old Wolf:
    > On Mar 3, 11:24 pm, "Alf P. Steinbach" <> wrote:
    >> Bas wrote:
    >>> 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

    --
    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?
     
    Alf P. Steinbach, Mar 4, 2007
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SpringFlowers AutumnMoon

    a different type of reference (shocked)

    SpringFlowers AutumnMoon, Sep 28, 2007, in forum: Ruby
    Replies:
    18
    Views:
    224
    Rick DeNatale
    Oct 2, 2007
Loading...

Share This Page