473,385 Members | 1,373 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How much is C# slower than C++?

Hi,

How much is C# slower than C++?

TIA
Roy
Nov 17 '05 #1
14 2336

"Roy Gourgi" <ro***@videotron.ca> wrote in message
news:4X*******************@wagner.videotron.net...
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.
Nov 17 '05 #2
Michael A. Covington wrote:
"Roy Gourgi" <ro***@videotron.ca> wrote in message
news:4X*******************@wagner.videotron.net...
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.


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.
Nov 17 '05 #3

"jeremiah johnson" <na*******@gmail.com> wrote in message
news:uX*************@TK2MSFTNGP10.phx.gbl...
Michael A. Covington wrote:
"Roy Gourgi" <ro***@videotron.ca> wrote in message
news:4X*******************@wagner.videotron.net...
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.


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.
Nov 17 '05 #4
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/

Nov 17 '05 #5
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

jeremiah johnson wrote:
Michael A. Covington wrote:
"Roy Gourgi" <ro***@videotron.ca> wrote in message
news:4X*******************@wagner.videotron.net...
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.

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.

Nov 17 '05 #6
http://msdn.microsoft.com/library/de...etperftips.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.

"Roy Gourgi" <ro***@videotron.ca> wrote in message
news:4X*******************@wagner.videotron.net...
Hi,

How much is C# slower than C++?

TIA
Roy

Nov 17 '05 #7
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" <ro***@videotron.ca> wrote in message
news:4X*******************@wagner.videotron.net...
Hi,

How much is C# slower than C++?

TIA
Roy

Nov 17 '05 #8
>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.

sh**********@yahoo.com wrote:
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/


Nov 17 '05 #9
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.

"Michael A. Covington" <lo**@ai.uga.edu.for.address> wrote in message
news:ek**************@TK2MSFTNGP14.phx.gbl...

"jeremiah johnson" <na*******@gmail.com> wrote in message
news:uX*************@TK2MSFTNGP10.phx.gbl...
Michael A. Covington wrote:
"Roy Gourgi" <ro***@videotron.ca> wrote in message
news:4X*******************@wagner.videotron.net...

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.


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.

Nov 17 '05 #10
jeremiah johnson <na*******@gmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #11
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

Bob Powell [MVP] wrote:
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.



Nov 17 '05 #12
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" <sd******@gmail.HEY_YOU.com> wrote in message
news:eP**************@TK2MSFTNGP09.phx.gbl...
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" <ro***@videotron.ca> wrote in message
news:4X*******************@wagner.videotron.net...
Hi,

How much is C# slower than C++?

TIA
Roy


Nov 17 '05 #13
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" <sd******@gmail.HEY_YOU.com> wrote in message
news:eY**************@TK2MSFTNGP10.phx.gbl...
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" <sd******@gmail.HEY_YOU.com> wrote in message
news:eP**************@TK2MSFTNGP09.phx.gbl...
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" <ro***@videotron.ca> wrote in message
news:4X*******************@wagner.videotron.net...
Hi,

How much is C# slower than C++?

TIA
Roy



Nov 17 '05 #14
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?.

Nov 17 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

114
by: Maurice LING | last post by:
This may be a dumb thing to ask, but besides the penalty for dynamic typing, is there any other real reasons that Python is slower than Java? maurice
5
by: Michael | last post by:
i experience slower compile times with VC++ 2003 compared to VC+6.0. Anyone experiencing the same? Should that be expected? This ineed matters, when total compilation time is > 1h and you have to...
87
by: John Rivers | last post by:
Hello everybody, I just wondered if anybody else has noticed this? It takes around 6 seconds to start debugging a very simple ASPX page with VS.NET whereas VB6 takes under 0.5 seconds, even...
77
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html The above link shows that C# is 450% slower on something as simple as a nested loop....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.