Visual C++ 7 speed vs Visual C# speed 
Author Message
 Visual C++ 7 speed vs Visual C# speed

Hi

I did a rough experiment the other day with strange results

I created two console apps one is C# and the other if Visual C++ 7

The apps do calculations for Generation of the Mandelbrot Set and time how
long it takes

The strange thing is that the Visual C# App is much faster than the C++ App.
( 1.5 secs per set for the C# App and 1.9 secs per set for the C++ App )
This seems totally bizarre to me as I would have thought it would be the
opposite

It puts me in the strange situation of thinking about rewritting my C++
fractal generator in C# to get more speed !!

The code follows in case anyone can see why the C++ code is so slow

//********** C++ VERSION ***********

#include "stdafx.h"
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

void main( void )
{
     int imgX;   //image width
     int imgY;   //image height
     double xMin;  //smallest real value
     double xMax;  //largest real value
     double yMin;  //smallest imaginary value
     double yMax;  //largest imaginary value
     int maxIterations; //maximum number of iterations
     long calcsPerformed;
     int iCalcs;
     clock_t start, finish;
     double  duration, total=0.0;

    for(int i=0;i<10;i++)
    {
      printf( "Time to do %d Calculations %d is ", iCalcs, i+1 );
      start = clock();
      imgX=320;
      imgY=240;
      xMin = -2.0;
      xMax = 1.25;
      yMin = -1.25;
      yMax = 1.25;
      maxIterations=4096;

      double oldX;  //temporary variable to store x-value
      double fx,fy;
      int m;    //variable to store number of iterations
      double dx=(xMax-xMin)/imgX; //how much to add for each x-pixel
      double dy=(yMax-yMin)/imgY; //how much to add for each y-pixe
      int px;    //Variable storing current x-pixel
      int py=0;   //Variable storing current y-pixel
      double x;   //Variable storing current x-value
      double y=yMin;  //Variable storing current y-value

      calcsPerformed=0;
      while (py<imgY)
      {
           px=0;
           x=xMin;
           py++;
           while (px<imgX)
            {
                fx=0;
                fy=0;
                m=0;
                do
                {
                     oldX=fx;
                     fx=fx*fx-fy*fy+x;
                     fy=2*oldX*fy+y;
                     m++;
                } while (((fx*fx+fy*fy)<4) && (m<maxIterations));
                calcsPerformed+=m;
                m*=m;
                px++;
                x+=dx;
           }
           y+=dy;
      }
      iCalcs = calcsPerformed;

       finish = clock();
       duration = (double)(finish - start) / CLOCKS_PER_SEC;
       total+=duration;
       printf( "%2.6f seconds\n", duration );
     }
     printf( "\nAverage time =%2.6f seconds\n\n",total/10);

Quote:
}

//********** END OF C++ VERSION ***********

//********** C# VERSION ***********
using System;

namespace PerformCS
{
    class Class1
     {
          [STAThread]
          static void Main(string[] args)
          {
               int imgX;   //image width
               int imgY;   //image height
               double xMin;  //smallest real value
               double xMax;  //largest real value
               double yMin;  //smallest imaginary value
               double yMax;  //largest imaginary value
               int maxIterations; //maximum number of iterations
               int calcsPerformed;
               int iCalcs = 0;

               DateTime start, finish;
               double  total=0.0;
               double duration;

               for(int i=0;i<10;i++)
               {
                    Console.WriteLine( "Time to do {0} Calculations {1} is
", iCalcs, i+1 );
                    start = DateTime.Now;
                    imgX=320;
                    imgY=240;
                    xMin = -2.0;
                    xMax = 1.25;
                    yMin = -1.25;
                    yMax = 1.25;
                    maxIterations=4096;

                    double oldX;  //temporary variable to store x-value
                    double fx,fy;
                    int m;    //variable to store number of iterations
                    double dx=(xMax-xMin)/imgX; //how much to add for each
x-pixel
                    double dy=(yMax-yMin)/imgY; //how much to add for each
y-pixe
                    int px;    //Variable storing current x-pixel
                    int py=0;   //Variable storing current y-pixel
                    double x;   //Variable storing current x-value
                    double y=yMin;  //Variable storing current y-value

                    calcsPerformed=0;
                    while (py<imgY)
                    {
                         px=0;
                         x=xMin;
                         py++;
                         while (px<imgX)
                         {
                              fx=0;
                              fy=0;
                              m=0;
                              do
                              {
                                   oldX=fx;
                                    fx=fx*fx-fy*fy+x;
                                   fy=2*oldX*fy+y;
                                   m++;
                              } while (((fx*fx+fy*fy)<4) &&
(m<maxIterations));
                              calcsPerformed+=m;
                              m*=m;
                              px++;
                              x+=dx;
                         }
                     y+=dy;
                }
                iCalcs = calcsPerformed;

                finish = DateTime.Now;
                duration = (double)(finish.Ticks - start.Ticks) / 10000000;
                total+=duration;
                Console.WriteLine( "{0:f} seconds\n", duration );
           }
           Console.WriteLine( "\nAverage time ={0:f} seconds\n\n",total/10);
        }
     }

Quote:
}

//********** END OF C# VERSION ***********


Sat, 15 Jan 2005 13:10:14 GMT  
 Visual C++ 7 speed vs Visual C# speed
That is interesting, yes :)  Apparently, the .NET framework JIT is better at
generating floating point code than the VC7 back end.  Interesting!

-cd

PS. I was able to confirm your findings on my machine - C# was about 10%
faster than C++ compiled with /O2 or /Ox.  I'm on a P-II machine - I'm guess
you're on a ~1Ghz machine, P-II or P-IV based on the relative times I saw.


Quote:
> Hi

> I did a rough experiment the other day with strange results

> I created two console apps one is C# and the other if Visual C++ 7

> The apps do calculations for Generation of the Mandelbrot Set and time how
> long it takes

> The strange thing is that the Visual C# App is much faster than the C++
App.
> ( 1.5 secs per set for the C# App and 1.9 secs per set for the C++ App )
> This seems totally bizarre to me as I would have thought it would be the
> opposite

> It puts me in the strange situation of thinking about rewritting my C++
> fractal generator in C# to get more speed !!

> The code follows in case anyone can see why the C++ code is so slow

> //********** C++ VERSION ***********

> #include "stdafx.h"
> #include <time.h>
> #include <stdio.h>
> #include <stdlib.h>

> void main( void )
> {
>      int imgX;   //image width
>      int imgY;   //image height
>      double xMin;  //smallest real value
>      double xMax;  //largest real value
>      double yMin;  //smallest imaginary value
>      double yMax;  //largest imaginary value
>      int maxIterations; //maximum number of iterations
>      long calcsPerformed;
>      int iCalcs;
>      clock_t start, finish;
>      double  duration, total=0.0;

>     for(int i=0;i<10;i++)
>     {
>       printf( "Time to do %d Calculations %d is ", iCalcs, i+1 );
>       start = clock();
>       imgX=320;
>       imgY=240;
>       xMin = -2.0;
>       xMax = 1.25;
>       yMin = -1.25;
>       yMax = 1.25;
>       maxIterations=4096;

>       double oldX;  //temporary variable to store x-value
>       double fx,fy;
>       int m;    //variable to store number of iterations
>       double dx=(xMax-xMin)/imgX; //how much to add for each x-pixel
>       double dy=(yMax-yMin)/imgY; //how much to add for each y-pixe
>       int px;    //Variable storing current x-pixel
>       int py=0;   //Variable storing current y-pixel
>       double x;   //Variable storing current x-value
>       double y=yMin;  //Variable storing current y-value

>       calcsPerformed=0;
>       while (py<imgY)
>       {
>            px=0;
>            x=xMin;
>            py++;
>            while (px<imgX)
>             {
>                 fx=0;
>                 fy=0;
>                 m=0;
>                 do
>                 {
>                      oldX=fx;
>                      fx=fx*fx-fy*fy+x;
>                      fy=2*oldX*fy+y;
>                      m++;
>                 } while (((fx*fx+fy*fy)<4) && (m<maxIterations));
>                 calcsPerformed+=m;
>                 m*=m;
>                 px++;
>                 x+=dx;
>            }
>            y+=dy;
>       }
>       iCalcs = calcsPerformed;

>        finish = clock();
>        duration = (double)(finish - start) / CLOCKS_PER_SEC;
>        total+=duration;
>        printf( "%2.6f seconds\n", duration );
>      }
>      printf( "\nAverage time =%2.6f seconds\n\n",total/10);
> }

> //********** END OF C++ VERSION ***********

> //********** C# VERSION ***********
> using System;

> namespace PerformCS
> {
>     class Class1
>      {
>           [STAThread]
>           static void Main(string[] args)
>           {
>                int imgX;   //image width
>                int imgY;   //image height
>                double xMin;  //smallest real value
>                double xMax;  //largest real value
>                double yMin;  //smallest imaginary value
>                double yMax;  //largest imaginary value
>                int maxIterations; //maximum number of iterations
>                int calcsPerformed;
>                int iCalcs = 0;

>                DateTime start, finish;
>                double  total=0.0;
>                double duration;

>                for(int i=0;i<10;i++)
>                {
>                     Console.WriteLine( "Time to do {0} Calculations {1} is
> ", iCalcs, i+1 );
>                     start = DateTime.Now;
>                     imgX=320;
>                     imgY=240;
>                     xMin = -2.0;
>                     xMax = 1.25;
>                     yMin = -1.25;
>                     yMax = 1.25;
>                     maxIterations=4096;

>                     double oldX;  //temporary variable to store x-value
>                     double fx,fy;
>                     int m;    //variable to store number of iterations
>                     double dx=(xMax-xMin)/imgX; //how much to add for each
> x-pixel
>                     double dy=(yMax-yMin)/imgY; //how much to add for each
> y-pixe
>                     int px;    //Variable storing current x-pixel
>                     int py=0;   //Variable storing current y-pixel
>                     double x;   //Variable storing current x-value
>                     double y=yMin;  //Variable storing current y-value

>                     calcsPerformed=0;
>                     while (py<imgY)
>                     {
>                          px=0;
>                          x=xMin;
>                          py++;
>                          while (px<imgX)
>                          {
>                               fx=0;
>                               fy=0;
>                               m=0;
>                               do
>                               {
>                                    oldX=fx;
>                                     fx=fx*fx-fy*fy+x;
>                                    fy=2*oldX*fy+y;
>                                    m++;
>                               } while (((fx*fx+fy*fy)<4) &&
> (m<maxIterations));
>                               calcsPerformed+=m;
>                               m*=m;
>                               px++;
>                               x+=dx;
>                          }
>                      y+=dy;
>                 }
>                 iCalcs = calcsPerformed;

>                 finish = DateTime.Now;
>                 duration = (double)(finish.Ticks - start.Ticks) /
10000000;
>                 total+=duration;
>                 Console.WriteLine( "{0:f} seconds\n", duration );
>            }
>            Console.WriteLine( "\nAverage time ={0:f}

seconds\n\n",total/10);

- Show quoted text -

Quote:
>         }
>      }
> }

> //********** END OF C# VERSION ***********



Sat, 15 Jan 2005 14:10:22 GMT  
 Visual C++ 7 speed vs Visual C# speed
On Tue, 30 Jul 2002 17:10:14 +1200, "Simon Knox"

Quote:

>The strange thing is that the Visual C# App is much faster than the C++ App.
>( 1.5 secs per set for the C# App and 1.9 secs per set for the C++ App )
>This seems totally bizarre to me as I would have thought it would be the
>opposite

In my experience, C++ easily beats C# in numerical computing.  Your
example is indeed very bizarre.  I could confirm it on my P4 machine
(1.66 sec for C++ vs 1.34 sec for C#) but I can't explain it -- your
code must have hit some kind of sweet spot of the JITter!

Doubling imgX and imgY produced the same result (6.66 vs 5.36, again
C# beat C++ by about 24%).  This was with -Ox -G6 for VC7 and /debug-
/optimize+ for C#.

Interestingly, targeting the CLR with VC7 (-clr -Ox -G6) produced much
*worse* results -- ca 4.88 seconds with the original imgX/imgY!
I can't even imagine begin to imagine an explanation for this.

I'm pretty sure Intel C++ should be able to beat both VC7 and C# at
this test, though.  Anyone got an installation handy?



Sat, 15 Jan 2005 17:29:37 GMT  
 Visual C++ 7 speed vs Visual C# speed
maybe C# use FPU registers for doubles and your compiled C++ doesn't ?

spartaco


Quote:
> Hi

> I did a rough experiment the other day with strange results

> I created two console apps one is C# and the other if Visual C++ 7

> The apps do calculations for Generation of the Mandelbrot Set and time how
> long it takes

> The strange thing is that the Visual C# App is much faster than the C++
App.
> ( 1.5 secs per set for the C# App and 1.9 secs per set for the C++ App )
> This seems totally bizarre to me as I would have thought it would be the
> opposite

> It puts me in the strange situation of thinking about rewritting my C++
> fractal generator in C# to get more speed !!

> The code follows in case anyone can see why the C++ code is so slow

> //********** C++ VERSION ***********

> #include "stdafx.h"
> #include <time.h>
> #include <stdio.h>
> #include <stdlib.h>

> void main( void )
> {
>      int imgX;   //image width
>      int imgY;   //image height
>      double xMin;  //smallest real value
>      double xMax;  //largest real value
>      double yMin;  //smallest imaginary value
>      double yMax;  //largest imaginary value
>      int maxIterations; //maximum number of iterations
>      long calcsPerformed;
>      int iCalcs;
>      clock_t start, finish;
>      double  duration, total=0.0;

>     for(int i=0;i<10;i++)
>     {
>       printf( "Time to do %d Calculations %d is ", iCalcs, i+1 );
>       start = clock();
>       imgX=320;
>       imgY=240;
>       xMin = -2.0;
>       xMax = 1.25;
>       yMin = -1.25;
>       yMax = 1.25;
>       maxIterations=4096;

>       double oldX;  //temporary variable to store x-value
>       double fx,fy;
>       int m;    //variable to store number of iterations
>       double dx=(xMax-xMin)/imgX; //how much to add for each x-pixel
>       double dy=(yMax-yMin)/imgY; //how much to add for each y-pixe
>       int px;    //Variable storing current x-pixel
>       int py=0;   //Variable storing current y-pixel
>       double x;   //Variable storing current x-value
>       double y=yMin;  //Variable storing current y-value

>       calcsPerformed=0;
>       while (py<imgY)
>       {
>            px=0;
>            x=xMin;
>            py++;
>            while (px<imgX)
>             {
>                 fx=0;
>                 fy=0;
>                 m=0;
>                 do
>                 {
>                      oldX=fx;
>                      fx=fx*fx-fy*fy+x;
>                      fy=2*oldX*fy+y;
>                      m++;
>                 } while (((fx*fx+fy*fy)<4) && (m<maxIterations));
>                 calcsPerformed+=m;
>                 m*=m;
>                 px++;
>                 x+=dx;
>            }
>            y+=dy;
>       }
>       iCalcs = calcsPerformed;

>        finish = clock();
>        duration = (double)(finish - start) / CLOCKS_PER_SEC;
>        total+=duration;
>        printf( "%2.6f seconds\n", duration );
>      }
>      printf( "\nAverage time =%2.6f seconds\n\n",total/10);
> }

> //********** END OF C++ VERSION ***********

> //********** C# VERSION ***********
> using System;

> namespace PerformCS
> {
>     class Class1
>      {
>           [STAThread]
>           static void Main(string[] args)
>           {
>                int imgX;   //image width
>                int imgY;   //image height
>                double xMin;  //smallest real value
>                double xMax;  //largest real value
>                double yMin;  //smallest imaginary value
>                double yMax;  //largest imaginary value
>                int maxIterations; //maximum number of iterations
>                int calcsPerformed;
>                int iCalcs = 0;

>                DateTime start, finish;
>                double  total=0.0;
>                double duration;

>                for(int i=0;i<10;i++)
>                {
>                     Console.WriteLine( "Time to do {0} Calculations {1} is
> ", iCalcs, i+1 );
>                     start = DateTime.Now;
>                     imgX=320;
>                     imgY=240;
>                     xMin = -2.0;
>                     xMax = 1.25;
>                     yMin = -1.25;
>                     yMax = 1.25;
>                     maxIterations=4096;

>                     double oldX;  //temporary variable to store x-value
>                     double fx,fy;
>                     int m;    //variable to store number of iterations
>                     double dx=(xMax-xMin)/imgX; //how much to add for each
> x-pixel
>                     double dy=(yMax-yMin)/imgY; //how much to add for each
> y-pixe
>                     int px;    //Variable storing current x-pixel
>                     int py=0;   //Variable storing current y-pixel
>                     double x;   //Variable storing current x-value
>                     double y=yMin;  //Variable storing current y-value

>                     calcsPerformed=0;
>                     while (py<imgY)
>                     {
>                          px=0;
>                          x=xMin;
>                          py++;
>                          while (px<imgX)
>                          {
>                               fx=0;
>                               fy=0;
>                               m=0;
>                               do
>                               {
>                                    oldX=fx;
>                                     fx=fx*fx-fy*fy+x;
>                                    fy=2*oldX*fy+y;
>                                    m++;
>                               } while (((fx*fx+fy*fy)<4) &&
> (m<maxIterations));
>                               calcsPerformed+=m;
>                               m*=m;
>                               px++;
>                               x+=dx;
>                          }
>                      y+=dy;
>                 }
>                 iCalcs = calcsPerformed;

>                 finish = DateTime.Now;
>                 duration = (double)(finish.Ticks - start.Ticks) /
10000000;
>                 total+=duration;
>                 Console.WriteLine( "{0:f} seconds\n", duration );
>            }
>            Console.WriteLine( "\nAverage time ={0:f}

seconds\n\n",total/10);

- Show quoted text -

Quote:
>         }
>      }
> }

> //********** END OF C# VERSION ***********



Sat, 15 Jan 2005 19:29:04 GMT  
 Visual C++ 7 speed vs Visual C# speed

Quote:
> The strange thing is that the Visual C# App is much faster than the C++
App.
> ( 1.5 secs per set for the C# App and 1.9 secs per set for the C++ App )
> This seems totally bizarre to me as I would have thought it would be the
> opposite

This contradicts my own, somewhat contrieved, performance tests. I've mainly
been toying with the most common vector operations in 3d-graphics. In those
tests C++ easily beats C# with a speed advantage of 200%-300%.

Quote:
> It puts me in the strange situation of thinking about rewritting my C++
> fractal generator in C# to get more speed !!

> The code follows in case anyone can see why the C++ code is so slow

I can confirm your strange results on my slightly oc Athlon XP1600+.
However, I also compiled the test with the Intel C++ 6.0 compiler, which I'm
using because it's has much better C++ standard conformance and generates
faster code. Here's the results:

Intel C++ 6.0 compiler: 0.60s
VC7 compiler: 1.35s
CS compiler: 0.91s

Btw: I used the default optimization switches.

Bo-Staffan



Sat, 15 Jan 2005 19:39:09 GMT  
 Visual C++ 7 speed vs Visual C# speed


Quote:
>                 do
>                 {
>                      oldX=fx;
>                      fx=fx*fx-fy*fy+x;
>                      fy=2*oldX*fy+y;
>                      m++;
>                 } while (((fx*fx+fy*fy)<4) && (m<maxIterations));

I changed this do-while loop to this (fx, fy and m are initialised to 0 so
it's always true on the first pass).

while(((fx*fx+fy*fy) < 4.0) && (m<maxIterations))
{
    oldX=fx;
    fx=fx*fx-fy*fy+x;
    fy=2*oldX*fy+y;
    m++;

Quote:
}

The c++ comes out approx 10% faster than the c# ( c++ - 0.85, c# - 0.96 on
Athlon 1.4), as I know next to nothing about how compilers optimize I don't
know why a do-while loop would be 50% slower than a while loop.

Dave



Sat, 15 Jan 2005 20:41:07 GMT  
 Visual C++ 7 speed vs Visual C# speed
On Tue, 30 Jul 2002 13:39:09 +0200, "Bo-Staffan Lankinen"

Quote:

>However, I also compiled the test with the Intel C++ 6.0 compiler, which I'm
>using because it's has much better C++ standard conformance and generates
>faster code. Here's the results:

>Intel C++ 6.0 compiler: 0.60s
>VC7 compiler: 1.35s
>CS compiler: 0.91s

That sounds more in line with what I would have expected.  From this
result, Dibgy's result, and my own experiment with MC++ I guess we
have to conclude that the culprit is a broken VC++ 7.0 optimizer...


Sun, 16 Jan 2005 00:00:58 GMT  
 Visual C++ 7 speed vs Visual C# speed

Quote:
> On Tue, 30 Jul 2002 13:39:09 +0200, "Bo-Staffan Lankinen"

> >However, I also compiled the test with the Intel C++ 6.0 compiler, which
I'm
> >using because it's has much better C++ standard conformance and generates
> >faster code. Here's the results:

> >Intel C++ 6.0 compiler: 0.60s
> >VC7 compiler: 1.35s
> >CS compiler: 0.91s

> That sounds more in line with what I would have expected.  From this
> result, Dibgy's result, and my own experiment with MC++ I guess we
> have to conclude that the culprit is a broken VC++ 7.0 optimizer...

And at that, the EXE produced by VC7 is about 10% faster than the same code
built with VC6 (for this particular benchmark).
-cd


Sun, 16 Jan 2005 00:16:20 GMT  
 Visual C++ 7 speed vs Visual C# speed
Hi

Thanks for testing out my code on different systems. I am pleased to know
that I am not going crazy.

I have a question about the Intel C++ 6.0 compiler. The machine that I did
the tests on is a Pentium 4 1.6GHz but my machine at home is an AMD Athlon
1.4GHz. would the Intel C++ compiler generate faster code for this machine
also or are the speed increases limited to Intel chips.

Simon



Sun, 16 Jan 2005 06:35:53 GMT  
 Visual C++ 7 speed vs Visual C# speed
What are the compiler options for the C++ build that you timed?

Ronald Laeremans
Visual C++ compiler and libraries team


Quote:
> Hi

> I did a rough experiment the other day with strange results

> I created two console apps one is C# and the other if Visual C++ 7

> The apps do calculations for Generation of the Mandelbrot Set and time how
> long it takes

> The strange thing is that the Visual C# App is much faster than the C++
App.
> ( 1.5 secs per set for the C# App and 1.9 secs per set for the C++ App )
> This seems totally bizarre to me as I would have thought it would be the
> opposite

> It puts me in the strange situation of thinking about rewritting my C++
> fractal generator in C# to get more speed !!

> The code follows in case anyone can see why the C++ code is so slow

> //********** C++ VERSION ***********

> #include "stdafx.h"
> #include <time.h>
> #include <stdio.h>
> #include <stdlib.h>

> void main( void )
> {
>      int imgX;   //image width
>      int imgY;   //image height
>      double xMin;  //smallest real value
>      double xMax;  //largest real value
>      double yMin;  //smallest imaginary value
>      double yMax;  //largest imaginary value
>      int maxIterations; //maximum number of iterations
>      long calcsPerformed;
>      int iCalcs;
>      clock_t start, finish;
>      double  duration, total=0.0;

>     for(int i=0;i<10;i++)
>     {
>       printf( "Time to do %d Calculations %d is ", iCalcs, i+1 );
>       start = clock();
>       imgX=320;
>       imgY=240;
>       xMin = -2.0;
>       xMax = 1.25;
>       yMin = -1.25;
>       yMax = 1.25;
>       maxIterations=4096;

>       double oldX;  //temporary variable to store x-value
>       double fx,fy;
>       int m;    //variable to store number of iterations
>       double dx=(xMax-xMin)/imgX; //how much to add for each x-pixel
>       double dy=(yMax-yMin)/imgY; //how much to add for each y-pixe
>       int px;    //Variable storing current x-pixel
>       int py=0;   //Variable storing current y-pixel
>       double x;   //Variable storing current x-value
>       double y=yMin;  //Variable storing current y-value

>       calcsPerformed=0;
>       while (py<imgY)
>       {
>            px=0;
>            x=xMin;
>            py++;
>            while (px<imgX)
>             {
>                 fx=0;
>                 fy=0;
>                 m=0;
>                 do
>                 {
>                      oldX=fx;
>                      fx=fx*fx-fy*fy+x;
>                      fy=2*oldX*fy+y;
>                      m++;
>                 } while (((fx*fx+fy*fy)<4) && (m<maxIterations));
>                 calcsPerformed+=m;
>                 m*=m;
>                 px++;
>                 x+=dx;
>            }
>            y+=dy;
>       }
>       iCalcs = calcsPerformed;

>        finish = clock();
>        duration = (double)(finish - start) / CLOCKS_PER_SEC;
>        total+=duration;
>        printf( "%2.6f seconds\n", duration );
>      }
>      printf( "\nAverage time =%2.6f seconds\n\n",total/10);
> }

> //********** END OF C++ VERSION ***********

> //********** C# VERSION ***********
> using System;

> namespace PerformCS
> {
>     class Class1
>      {
>           [STAThread]
>           static void Main(string[] args)
>           {
>                int imgX;   //image width
>                int imgY;   //image height
>                double xMin;  //smallest real value
>                double xMax;  //largest real value
>                double yMin;  //smallest imaginary value
>                double yMax;  //largest imaginary value
>                int maxIterations; //maximum number of iterations
>                int calcsPerformed;
>                int iCalcs = 0;

>                DateTime start, finish;
>                double  total=0.0;
>                double duration;

>                for(int i=0;i<10;i++)
>                {
>                     Console.WriteLine( "Time to do {0} Calculations {1} is
> ", iCalcs, i+1 );
>                     start = DateTime.Now;
>                     imgX=320;
>                     imgY=240;
>                     xMin = -2.0;
>                     xMax = 1.25;
>                     yMin = -1.25;
>                     yMax = 1.25;
>                     maxIterations=4096;

>                     double oldX;  //temporary variable to store x-value
>                     double fx,fy;
>                     int m;    //variable to store number of iterations
>                     double dx=(xMax-xMin)/imgX; //how much to add for each
> x-pixel
>                     double dy=(yMax-yMin)/imgY; //how much to add for each
> y-pixe
>                     int px;    //Variable storing current x-pixel
>                     int py=0;   //Variable storing current y-pixel
>                     double x;   //Variable storing current x-value
>                     double y=yMin;  //Variable storing current y-value

>                     calcsPerformed=0;
>                     while (py<imgY)
>                     {
>                          px=0;
>                          x=xMin;
>                          py++;
>                          while (px<imgX)
>                          {
>                               fx=0;
>                               fy=0;
>                               m=0;
>                               do
>                               {
>                                    oldX=fx;
>                                     fx=fx*fx-fy*fy+x;
>                                    fy=2*oldX*fy+y;
>                                    m++;
>                               } while (((fx*fx+fy*fy)<4) &&
> (m<maxIterations));
>                               calcsPerformed+=m;
>                               m*=m;
>                               px++;
>                               x+=dx;
>                          }
>                      y+=dy;
>                 }
>                 iCalcs = calcsPerformed;

>                 finish = DateTime.Now;
>                 duration = (double)(finish.Ticks - start.Ticks) /
10000000;
>                 total+=duration;
>                 Console.WriteLine( "{0:f} seconds\n", duration );
>            }
>            Console.WriteLine( "\nAverage time ={0:f}

seconds\n\n",total/10);

- Show quoted text -

Quote:
>         }
>      }
> }

> //********** END OF C# VERSION ***********



Tue, 18 Jan 2005 01:58:13 GMT  
 Visual C++ 7 speed vs Visual C# speed
Sorry, I just saw that info later. I will have someone look at this.

Ronald Laeremans
Visual C++ compiler and libraries team



Quote:
> What are the compiler options for the C++ build that you timed?

> Ronald Laeremans
> Visual C++ compiler and libraries team



> > Hi

> > I did a rough experiment the other day with strange results

> > I created two console apps one is C# and the other if Visual C++ 7

> > The apps do calculations for Generation of the Mandelbrot Set and time
how
> > long it takes

> > The strange thing is that the Visual C# App is much faster than the C++
> App.
> > ( 1.5 secs per set for the C# App and 1.9 secs per set for the C++ App )
> > This seems totally bizarre to me as I would have thought it would be the
> > opposite

> > It puts me in the strange situation of thinking about rewritting my C++
> > fractal generator in C# to get more speed !!

> > The code follows in case anyone can see why the C++ code is so slow

> > //********** C++ VERSION ***********

> > #include "stdafx.h"
> > #include <time.h>
> > #include <stdio.h>
> > #include <stdlib.h>

> > void main( void )
> > {
> >      int imgX;   //image width
> >      int imgY;   //image height
> >      double xMin;  //smallest real value
> >      double xMax;  //largest real value
> >      double yMin;  //smallest imaginary value
> >      double yMax;  //largest imaginary value
> >      int maxIterations; //maximum number of iterations
> >      long calcsPerformed;
> >      int iCalcs;
> >      clock_t start, finish;
> >      double  duration, total=0.0;

> >     for(int i=0;i<10;i++)
> >     {
> >       printf( "Time to do %d Calculations %d is ", iCalcs, i+1 );
> >       start = clock();
> >       imgX=320;
> >       imgY=240;
> >       xMin = -2.0;
> >       xMax = 1.25;
> >       yMin = -1.25;
> >       yMax = 1.25;
> >       maxIterations=4096;

> >       double oldX;  //temporary variable to store x-value
> >       double fx,fy;
> >       int m;    //variable to store number of iterations
> >       double dx=(xMax-xMin)/imgX; //how much to add for each x-pixel
> >       double dy=(yMax-yMin)/imgY; //how much to add for each y-pixe
> >       int px;    //Variable storing current x-pixel
> >       int py=0;   //Variable storing current y-pixel
> >       double x;   //Variable storing current x-value
> >       double y=yMin;  //Variable storing current y-value

> >       calcsPerformed=0;
> >       while (py<imgY)
> >       {
> >            px=0;
> >            x=xMin;
> >            py++;
> >            while (px<imgX)
> >             {
> >                 fx=0;
> >                 fy=0;
> >                 m=0;
> >                 do
> >                 {
> >                      oldX=fx;
> >                      fx=fx*fx-fy*fy+x;
> >                      fy=2*oldX*fy+y;
> >                      m++;
> >                 } while (((fx*fx+fy*fy)<4) && (m<maxIterations));
> >                 calcsPerformed+=m;
> >                 m*=m;
> >                 px++;
> >                 x+=dx;
> >            }
> >            y+=dy;
> >       }
> >       iCalcs = calcsPerformed;

> >        finish = clock();
> >        duration = (double)(finish - start) / CLOCKS_PER_SEC;
> >        total+=duration;
> >        printf( "%2.6f seconds\n", duration );
> >      }
> >      printf( "\nAverage time =%2.6f seconds\n\n",total/10);
> > }

> > //********** END OF C++ VERSION ***********

> > //********** C# VERSION ***********
> > using System;

> > namespace PerformCS
> > {
> >     class Class1
> >      {
> >           [STAThread]
> >           static void Main(string[] args)
> >           {
> >                int imgX;   //image width
> >                int imgY;   //image height
> >                double xMin;  //smallest real value
> >                double xMax;  //largest real value
> >                double yMin;  //smallest imaginary value
> >                double yMax;  //largest imaginary value
> >                int maxIterations; //maximum number of iterations
> >                int calcsPerformed;
> >                int iCalcs = 0;

> >                DateTime start, finish;
> >                double  total=0.0;
> >                double duration;

> >                for(int i=0;i<10;i++)
> >                {
> >                     Console.WriteLine( "Time to do {0} Calculations {1}
is
> > ", iCalcs, i+1 );
> >                     start = DateTime.Now;
> >                     imgX=320;
> >                     imgY=240;
> >                     xMin = -2.0;
> >                     xMax = 1.25;
> >                     yMin = -1.25;
> >                     yMax = 1.25;
> >                     maxIterations=4096;

> >                     double oldX;  //temporary variable to store x-value
> >                     double fx,fy;
> >                     int m;    //variable to store number of iterations
> >                     double dx=(xMax-xMin)/imgX; //how much to add for
each
> > x-pixel
> >                     double dy=(yMax-yMin)/imgY; //how much to add for
each
> > y-pixe
> >                     int px;    //Variable storing current x-pixel
> >                     int py=0;   //Variable storing current y-pixel
> >                     double x;   //Variable storing current x-value
> >                     double y=yMin;  //Variable storing current y-value

> >                     calcsPerformed=0;
> >                     while (py<imgY)
> >                     {
> >                          px=0;
> >                          x=xMin;
> >                          py++;
> >                          while (px<imgX)
> >                          {
> >                               fx=0;
> >                               fy=0;
> >                               m=0;
> >                               do
> >                               {
> >                                    oldX=fx;
> >                                     fx=fx*fx-fy*fy+x;
> >                                    fy=2*oldX*fy+y;
> >                                    m++;
> >                               } while (((fx*fx+fy*fy)<4) &&
> > (m<maxIterations));
> >                               calcsPerformed+=m;
> >                               m*=m;
> >                               px++;
> >                               x+=dx;
> >                          }
> >                      y+=dy;
> >                 }
> >                 iCalcs = calcsPerformed;

> >                 finish = DateTime.Now;
> >                 duration = (double)(finish.Ticks - start.Ticks) /
> 10000000;
> >                 total+=duration;
> >                 Console.WriteLine( "{0:f} seconds\n", duration );
> >            }
> >            Console.WriteLine( "\nAverage time ={0:f}
> seconds\n\n",total/10);
> >         }
> >      }
> > }

> > //********** END OF C# VERSION ***********



Tue, 18 Jan 2005 02:06:53 GMT  
 Visual C++ 7 speed vs Visual C# speed
Hi

Hope this doesn't just add more confusion but I have tracked down a
reproducable bit of code that shows both the do-while going slowly and also
quickly and have pasted the code below. To reproduce the problem just create
a basic console app and paste the below code into the main cpp file that's
created and compile in release, check the times, now comment out the end of
the do-while loop as commented in the code and recompile and you should see
a rather large degradation. All I (can remember that I) have done to the
original code is add the red, green and blue variables and add the ifs in
the loop (I wanted to see the pretty colours).

I hope it is as reproducable for you as it is for me.

Dave

// CODE STARTS HERE

// while-test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

void main( void )
{
 int imgX;   //image width
 int imgY;   //image height
 double xMin;  //smallest real value
 double xMax;  //largest real value
 double yMin;  //smallest imaginary value
 double yMax;  //largest imaginary value
 int maxIterations; //maximum number of iterations
 long calcsPerformed;
 int iCalcs = 0;
 clock_t start, finish;
 double  duration, total=0.0;

 char szMsg[1024];
 char szTmp[1024];
 memset(szMsg, 0, 1024);
 memset(szTmp, 0, 1024);

 for(int i=0;i<10;i++)
 {
  sprintf(szTmp, "Time to do %d Calculations %d is ", iCalcs, i+1 );
  strcat(szMsg, szTmp);
  start = clock();
  imgX=320;
  imgY=240;
  xMin = -2.0;
  xMax = 1.25;
  yMin = -1.25;
  yMax = 1.25;
  maxIterations=4096;

  double oldX;  //temporary variable to store x-value
  double fx,fy;
  int m;    //variable to store number of iterations
  double dx=(xMax-xMin)/imgX; //how much to add for each x-pixel
  double dy=(yMax-yMin)/imgY; //how much to add for each y-pixe
  int px;    //Variable storing current x-pixel
  int py=0;   //Variable storing current y-pixel
  double x;   //Variable storing current x-value
  double y=yMin;  //Variable storing current y-value

  int nRed = 0;
  int nGreen = 0;
  int nBlue = 0;

  calcsPerformed=0;
  while (py<imgY)
  {
   px=0;
   x=xMin;
   py++;
   while (px<imgX)
   {
    fx=0;
    fy=0;
    m=0;

    //while(((fx*fx+fy*fy) < 4.0) && (m<maxIterations))
    do
    {
     oldX=fx;
     fx=fx*fx-fy*fy+x;
     fy=2*oldX*fy+y;
     m++;

// comment out from here to end of do-while to experience 50% degradation

     if(nRed > 255)
     {
      nRed = 0;
      ++nGreen;
     }
     if(nGreen > 255)
     {
      nGreen = 0;
      ++nBlue;
     }
     if(nBlue > 255)
      nBlue = 0;

    } while(((fx*fx+fy*fy) < 4.0) && (m<maxIterations));

    calcsPerformed+=m;
    m*=m;
    px++;
    x+=dx;
   }
   y+=dy;
  }
  iCalcs = calcsPerformed;

  finish = clock();
  duration = (double)(finish - start) / CLOCKS_PER_SEC;
  total+=duration;
  sprintf(szTmp, "%2.6f seconds\n", duration );
  strcat(szMsg, szTmp);
 }
 sprintf(szTmp, "\nAverage time =%2.6f seconds\n\n",total/10);
 strcat(szMsg, szTmp);

 printf(szMsg);

Quote:
}



Tue, 18 Jan 2005 04:40:36 GMT  
 Visual C++ 7 speed vs Visual C# speed

Quote:

>Hope this doesn't just add more confusion but I have tracked down a
>reproducable bit of code that shows both the do-while going slowly and also
>quickly and have pasted the code below.

<snip>

I think it just displays that the floating-point handling of the
optimizer hasn't been changed much (or at all) since VC6, since VC6
exhibits the same behaviour.

What it boils down to is that using do..while the floating point
values are for some reason pushed on the stack, but in the while{}
case it uses the FPU registers.

--
Mike



Wed, 19 Jan 2005 01:54:45 GMT  
 
 [ 13 post ] 

 Relevant Pages 

1. Visual C++ vs Visual Basic vs Visual J++

2. Speed: C# vs. C++

3. Problem with Visual Studio .NET (C++) - Maximise Speed (/O2) - Unmanaged Code

4. Harddisk write speed problem in Visual C++

5. .NET speed vs COM speed

6. C++ memory access speeds: normal vs struct vs class

7. Visual C++ 6.0 vs. Visual C++ .NET

8. Visual C# .NET Standard vs. Visual Studio.NET

9. Speed vs C#

10. C# vs. Java: Graphics/Text Rendering Speed

11. Performance / speed change in VC++ between Visual Studio 97 and VS6.0

12. visual basic VS. visual C++

 

 
Powered by phpBB® Forum Software