How much is C# slower than C++?

M

Michael A. Covington

Roy Gourgi said:
Hi,

How much is C# slower than C++?

Depends on what you're doing. There no inherent reason for it to be slower
at all. MSIL is compiled into native machine code before it executes.
 
J

jeremiah johnson

Michael said:
Depends on what you're doing. There no inherent reason for it to be slower
at all. MSIL is compiled into native machine code before it executes.

That compilation takes place with a JIT (Just In Time) compiler, and
this adds time to the execution. It is noticeably slower.

How much slower though, depends on what you're doing. Some argue half
as fast, some argue 1/4 as fast. For most applications it will be more
than fast enough.
 
M

Michael A. Covington

jeremiah johnson said:
That compilation takes place with a JIT (Just In Time) compiler, and this
adds time to the execution. It is noticeably slower.

Only the first time through a method. Loops - which are the time-consuming
part - run at full speed after the first iteration.
 
S

shiv_koirala

http://www.kuro5hin.org/print/2002/6/25/122237/078
Tested with a common logic on 800 Mhz Pentium IV . All times in seconds

Standard C++: 27.99
Standard C++ + SGI STL 11.15
Standard C++ + SGI STL and hash_map 6.04
g++ C++: 17.28
g++ C++ + SGI STL: 14.93
g++ C++ + SGI STL and hash_map: 7.29
Standard C++ compiled /clr: 34.36
Standard C++ + SGI STL compiled /clr: 25.09
Standard C++ + SGI STL and hash_map compiled /clr: 12.98
Managed C++: 111.59
C#: 93.08
Java: 65.57

-------
Regards ,
C#, VB.NET , SQL SERVER , UML , DESIGN Patterns Interview question book
http://www.geocities.com/dotnetinterviews/
My Interview Blog
http://spaces.msn.com/members/dotnetinterviews/
 
B

Bob Grommes

JIT compilation happens only when a method is first called, not every
time and certainly not within loops. There is no inherent reason why,
after that initial penalty, C# would be slower than any other language
including C++. There are of course reasons besides inherent ones -- for
example, the C# compiler does not have as many sophisticated
optimizations as does the C++ compiler, although, they are adding more
all the time.

The answer as always, is "it all depends." For line of business
applications I've always been delighted with C#'s performance. I
probably wouldn't be satisfied if I were writing (say) real time apps.
I would be willing to say that C# is not going to result in overall real
world performance degradation over C++ for the vast majority of business
applications. I don't know who's arguing for a 50 to 75% performance
drop, but I would be curious to be pointed to their arguments. Chances
are they are contrived test scenarios, or just plain based on flawed
assumptions.

--Bob
 
F

Fred Mellender

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/dotnetperftips.asp

says:
----------------------
a.. Abstractions—This is an area where the beefy, slow C++ backend compiler
wins heavily over the JIT. If you wrap an int inside a class for abstraction
purposes, and you access it strictly as an int, the C++ compiler can reduce
the overhead of the wrapper to practically nothing. You can add many levels
of abstraction to the wrapper, without increasing the cost. The JIT is
unable to take the time necessary to eliminate this cost, making deep
abstractions more expensive in MC++.
a.. Floating Point—The v1 JIT does not currently perform all the FP-specific
optimizations that the VC++ backend does, making floating point operations
more expensive for now.
a.. Multidimensional Arrays—The JIT is better at handling jagged arrays than
multidimensional ones, so use jagged arrays instead.
a.. 64 bit Arithmetic—In future versions, 64-bit optimizations will be added
to the JIT.
 
S

Scott

As a "débutant" in c++, I did the following in c# and in c++, just to see
for myself:

namespace PerfTest
{
struct Obj {
public int a,b,c,d,e,f,g,h,i,j, k, l, m,n,o,p,q,r,s,t,u,v,w,x,y,z;
int get_i() { return i;}
}

class Class1
{
static void pass_by_value(Obj o) {
o.i++;
}
static void pass_by_ref(ref Obj o) {
o.i++;
}

[STAThread]
static void Main(string[] args)
{
Obj obj = new Obj();
DateTime start, finish;
TimeSpan duration;

Console.WriteLine("CSHARP test.");
Console.Write("How many? ");
int count = Convert.ToInt32(Console.ReadLine());


Console.Write("Test pass by value: ");
start = DateTime.Now;
for(int i=0; i<count; i++)
pass_by_value(obj);
finish = DateTime.Now;

duration = finish - start;
Console.WriteLine("{0} ms.", duration.Milliseconds);

Console.Write("Test pass by ref : ");
start = DateTime.Now;
for(int i=0; i<count; i++)
pass_by_ref(ref obj);
finish = DateTime.Now;
duration = finish - start;
Console.WriteLine("{0} ms.", duration.Milliseconds);



Console.Write("[Enter] to quit."); Console.ReadLine();
}
}
}


c# turned out to be 10x faster over 10e6 loops, but I suspect it was because
JIT (or whatever) optimized away a lot of the work I was trying to make the
computer do. I'm not saying c# is faster most of the time, but in my little
test, it was. Of course, startup time for c# apps is longer than for c++ for
the reasons mentioned elsewhere in this thread.

scott
 
D

Daniel Jin

From the simple fact that the author seems to think that just by
compiling c++ with /clr switch will generate IL code. I'd throw these
results out of the window. The author clearly didn't even know enough
about the platform at the time of the writing.

another problem with his straight porting technique is that he's
programming right into many known performance problems, like the hit
he's taking on boxing for example. not to mention the different APIs he
used across the platforms clearly had different implementations. I
don't see any validity in these tests.
 
B

Bob Powell [MVP]

A goal which the C# team aimed for and I think has consistently hit or
exeeded was to have a C# program at least 80% of the speed of the exact
equivalent C++ program. One of thier big claims to fame was the port of a
well known first-person shooter to C#.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
J

Jon Skeet [C# MVP]

jeremiah johnson said:
That compilation takes place with a JIT (Just In Time) compiler, and
this adds time to the execution. It is noticeably slower.

*Once*. After it's compiled it, there's no need for it to be slower,
beyond the fact that the JIT doesn't get as much time to optimise as a
C++ compiler typically gets. (It does have the advantage of knowing
exactly what hardware it's running on though.)
How much slower though, depends on what you're doing. Some argue half
as fast, some argue 1/4 as fast.

I've never seen figures like that - usually it's in the range of 5-10%
slower, if that.
 
B

Brian Gideon

Yes, and a 20% performance penalty is nothing. Use all that extra
money you saved in development costs and buy hardware that's 20%
faster. You'd still probably come out ahead.

Brian
 
S

Scott Coonce

just in case anybody comes along and reads this thread again, i made the
classic mistake of comparing the "debug" builds. so as i've read in other
threads, all timing comparison bets are off.

scott


Scott said:
As a "débutant" in c++, I did the following in c# and in c++, just to see
for myself:

namespace PerfTest
{
struct Obj {
public int a,b,c,d,e,f,g,h,i,j, k, l, m,n,o,p,q,r,s,t,u,v,w,x,y,z;
int get_i() { return i;}
}

class Class1
{
static void pass_by_value(Obj o) {
o.i++;
}
static void pass_by_ref(ref Obj o) {
o.i++;
}

[STAThread]
static void Main(string[] args)
{
Obj obj = new Obj();
DateTime start, finish;
TimeSpan duration;

Console.WriteLine("CSHARP test.");
Console.Write("How many? ");
int count = Convert.ToInt32(Console.ReadLine());


Console.Write("Test pass by value: ");
start = DateTime.Now;
for(int i=0; i<count; i++)
pass_by_value(obj);
finish = DateTime.Now;

duration = finish - start;
Console.WriteLine("{0} ms.", duration.Milliseconds);

Console.Write("Test pass by ref : ");
start = DateTime.Now;
for(int i=0; i<count; i++)
pass_by_ref(ref obj);
finish = DateTime.Now;
duration = finish - start;
Console.WriteLine("{0} ms.", duration.Milliseconds);



Console.Write("[Enter] to quit."); Console.ReadLine();
}
}
}


c# turned out to be 10x faster over 10e6 loops, but I suspect it was
because JIT (or whatever) optimized away a lot of the work I was trying to
make the computer do. I'm not saying c# is faster most of the time, but
in my little test, it was. Of course, startup time for c# apps is longer
than for c++ for the reasons mentioned elsewhere in this thread.

scott


Roy Gourgi said:
Hi,

How much is C# slower than C++?

TIA
Roy
 
A

Alvin Bruney - ASP.NET MVP

yup, debug timing data is pretty useless.

--
Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ www.lulu.com/owc
Forth-coming VSTO.NET - Wrox/Wiley 2006
-------------------------------------------------------



Scott Coonce said:
just in case anybody comes along and reads this thread again, i made the
classic mistake of comparing the "debug" builds. so as i've read in other
threads, all timing comparison bets are off.

scott


Scott said:
As a "débutant" in c++, I did the following in c# and in c++, just to see
for myself:

namespace PerfTest
{
struct Obj {
public int a,b,c,d,e,f,g,h,i,j, k, l, m,n,o,p,q,r,s,t,u,v,w,x,y,z;
int get_i() { return i;}
}

class Class1
{
static void pass_by_value(Obj o) {
o.i++;
}
static void pass_by_ref(ref Obj o) {
o.i++;
}

[STAThread]
static void Main(string[] args)
{
Obj obj = new Obj();
DateTime start, finish;
TimeSpan duration;

Console.WriteLine("CSHARP test.");
Console.Write("How many? ");
int count = Convert.ToInt32(Console.ReadLine());


Console.Write("Test pass by value: ");
start = DateTime.Now;
for(int i=0; i<count; i++)
pass_by_value(obj);
finish = DateTime.Now;

duration = finish - start;
Console.WriteLine("{0} ms.", duration.Milliseconds);

Console.Write("Test pass by ref : ");
start = DateTime.Now;
for(int i=0; i<count; i++)
pass_by_ref(ref obj);
finish = DateTime.Now;
duration = finish - start;
Console.WriteLine("{0} ms.", duration.Milliseconds);



Console.Write("[Enter] to quit."); Console.ReadLine();
}
}
}


c# turned out to be 10x faster over 10e6 loops, but I suspect it was
because JIT (or whatever) optimized away a lot of the work I was trying to
make the computer do. I'm not saying c# is faster most of the time, but
in my little test, it was. Of course, startup time for c# apps is longer
than for c++ for the reasons mentioned elsewhere in this thread.

scott


Roy Gourgi said:
Hi,

How much is C# slower than C++?

TIA
Roy
 
R

riscy

Wondering would .net 2.0 be any faster than .net 1.1 framework between
C++ and C#?.

Since the PC already powerful processor for majority of non-gamer
application, does it really matter that much?.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top