all groups > c# > october 2005 >
You're in the

c#

group:

How much is C# slower than C++?


Re: How much is C# slower than C++? shiv_koirala NO[at]SPAM yahoo.com
10/2/2005 8:27:07 PM
c#:
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/
Re: How much is C# slower than C++? Bob Grommes
10/2/2005 8:32:07 PM
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

[quoted text, click to view]
Re: How much is C# slower than C++? jeremiah johnson
10/2/2005 8:58:20 PM
[quoted text, click to view]

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
How much is C# slower than C++? Roy Gourgi
10/2/2005 9:36:51 PM
Hi,

How much is C# slower than C++?

TIA
Roy

Re: How much is C# slower than C++? Michael A. Covington
10/2/2005 9:48:13 PM

[quoted text, click to view]

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.

Re: How much is C# slower than C++? Michael A. Covington
10/2/2005 11:05:24 PM

[quoted text, click to view]

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

Re: How much is C# slower than C++? Scott
10/3/2005 12:00:00 AM
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


[quoted text, click to view]

Re: How much is C# slower than C++? Daniel Jin
10/3/2005 6:59:48 AM
[quoted text, click to view]
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.

[quoted text, click to view]
Re: How much is C# slower than C++? Fred Mellender
10/3/2005 9:31:23 AM
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.
------------------------------------------------

There are other useful comparisons in the same document. This document is
for V1 of C#. It is not clear what improvements were made in V2.

[quoted text, click to view]

Re: How much is C# slower than C++? Brian Gideon
10/3/2005 1:02:56 PM
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

[quoted text, click to view]
Re: How much is C# slower than C++? Jon Skeet [C# MVP]
10/3/2005 7:42:55 PM
[quoted text, click to view]

*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.)

[quoted text, click to view]

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

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Re: How much is C# slower than C++? Bob Powell [MVP]
10/3/2005 8:00:44 PM
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.





[quoted text, click to view]

Re: How much is C# slower than C++? Scott Coonce
11/1/2005 2:54:08 PM
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


[quoted text, click to view]

Re: How much is C# slower than C++? Alvin Bruney - ASP.NET MVP
11/2/2005 2:27:00 PM
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
-------------------------------------------------------



[quoted text, click to view]

Re: How much is C# slower than C++? riscy NO[at]SPAM onetel.com
11/2/2005 10:39:10 PM
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?.
AddThis Social Bookmark Button