By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,212 Members | 2,209 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,212 IT Pros & Developers. It's quick & easy.

Why is C# 500% slower than C++ on Nested Loops ???

P: n/a
http://www.tommti-systems.de/go.html...enchmarks.html
Why is C# 500% slower than C++ on Nested Loops ???
Will this problem be solved in the future???
Nov 17 '05 #1
Share this Question
Share on Google+
17 Replies


P: n/a
Peter,

Well ... you will note it's more than 3 times faster than Java, which is
a much more appropriate comparison. C++ and C# are different tools for
different purposes, and are most usefully compared to their peers, not
to each other.

That said, you're right that the gap was particularly wide in this case.
I can only guess that C# doesn't have as many compiler optimizations
in place as C++ that would benefit this particular scenario.

--Bob

Peter Olcott wrote:
http://www.tommti-systems.de/go.html...enchmarks.html
Why is C# 500% slower than C++ on Nested Loops ???
Will this problem be solved in the future???

Nov 17 '05 #2

P: n/a

"Bob Grommes" <bo*@bobgrommes.com> wrote in message news:uq**************@TK2MSFTNGP15.phx.gbl...
Peter,

Well ... you will note it's more than 3 times faster than Java, which is a much more appropriate comparison. C++ and C# are
different tools for different purposes, and are most usefully compared to their peers, not to each other.

That said, you're right that the gap was particularly wide in this case. I can only guess that C# doesn't have as many compiler
optimizations in place as C++ that would benefit this particular scenario.

--Bob

Peter Olcott wrote:
http://www.tommti-systems.de/go.html...enchmarks.html
Why is C# 500% slower than C++ on Nested Loops ???
Will this problem be solved in the future???

I am estimating that it will be a problem that is easy to fix.
I am making this estimate on the basis of how well C# did
on the other benchmarks, compared to Visual C++.
Nov 17 '05 #3

P: n/a
Peter,
Why is C# 500% slower than C++ on Nested Loops ???


If you are really worried about performance and still want to use managed
code, please read up on "ngen".

Regards,

Randy
Nov 17 '05 #4

P: n/a
Bob Grommes <bo*@bobgrommes.com> wrote:
Well ... you will note it's more than 3 times faster than Java, which is
a much more appropriate comparison.


Which result are you looking at? Looking at the "nested loops" column
(the one the OP is talking about) I can only see Java being faster than
C# - and in some cases the Java is faster than the C++.

Also note that the released Java 1.5 isn't part of the results, and
neither is the IBM JVM (which I believe would be a lot better at the
trig - I don't know about the other results).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5

P: n/a
Randy A. Ynchausti <ra*************@msn.com> wrote:
Why is C# 500% slower than C++ on Nested Loops ???


If you are really worried about performance and still want to use managed
code, please read up on "ngen".


That makes startup faster, but it actually reduces the number of
optimisations the JIT performs.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6

P: n/a
On Fri, 26 Aug 2005 21:29:31 -0500, "Peter Olcott" <ol****@att.net>
wrote:
I am estimating that it will be a problem that is easy to fix.
Uh, no. Writing a good optimizer is pretty hard. The current
iterations of Visual C++ and Intel C++ had over a decade of labor
poured into them to make them as fast as they are.
I am making this estimate on the basis of how well C# did
on the other benchmarks, compared to Visual C++.


You're comparing different benchmarks for languages with very
different infrastructures! You can't make any cross-inferences from
one benchmark to another in this case.
--
http://www.kynosarges.de
Nov 17 '05 #7

P: n/a
"Jon Skeet [C# MVP]" <sk***@pobox.com> ha scritto nel messaggio
Which result are you looking at? Looking at the "nested loops" column
(the one the OP is talking about) I can only see Java being faster than
C# - and in some cases the Java is faster than the C++.


Java faster than something else?
It's hard to me to figure it out :)

--
..Net Reporting tool: http://www.neodatatype.net
Nov 17 '05 #8

P: n/a

"Peter Olcott" <ol****@att.net> wrote in message
news:RQNPe.2261$UI.1020@okepread05...
http://www.tommti-systems.de/go.html...enchmarks.html
Why is C# 500% slower than C++ on Nested Loops ???
Will this problem be solved in the future???


In general, such kind of micro-benchmarks have little or no value, in this
particular case, it shows that the C# language compiler could have done a
better job in optimizing the IL.
To prove it, here are the results of the nested loop run comparing C++,
C++/CLI and C#.

//C++ native (Compiler version 13.00)
Nested Loop elapsed time: 6048 ms -1804337152
//C++ managed - compiler options /clr /O2 /EHsc (14.00)
Nested Loop elapsed time: 6047 ms -1804337152
//C#
v1.1
Nested Loop elapsed time: 19800 ms - -1804337152
v2.0
Nested Loop elapsed time: 21050 ms - -1804337152

See, the native C++ and managed C++ results are the same, this indicates
that it's not a JIT issue but an IL issue.
When comparing the generated IL of both C# and C++/CLI, it's the C++/CLI
language compiler who did a better job than C#. However, for the
'Exceptions" case, it's C# who is the clear winner (managed code), so
suppose this is the result of the differences in priorities and constraints
set by the different language teams.
Willy.
Nov 17 '05 #9

P: n/a

"Christoph Nahr" <ch************@kynosarges.de> wrote in message news:32********************************@4ax.com...
On Fri, 26 Aug 2005 21:29:31 -0500, "Peter Olcott" <ol****@att.net>
wrote:
I am estimating that it will be a problem that is easy to fix.


Uh, no. Writing a good optimizer is pretty hard. The current
iterations of Visual C++ and Intel C++ had over a decade of labor
poured into them to make them as fast as they are.


Yet, since these mechanisms already exist in code that Microsoft
already owns, it is only a matter of adapting and porting. By very
easy, I am meaning, "just takes a little work", as opposed to
requiring new computer science to be invented.
I am making this estimate on the basis of how well C# did
on the other benchmarks, compared to Visual C++.


You're comparing different benchmarks for languages with very
different infrastructures! You can't make any cross-inferences from
one benchmark to another in this case.
--
http://www.kynosarges.de

Nov 17 '05 #10

P: n/a

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message news:MP************************@msnews.microsoft.c om...
Bob Grommes <bo*@bobgrommes.com> wrote:
Well ... you will note it's more than 3 times faster than Java, which is
a much more appropriate comparison.
Which result are you looking at? Looking at the "nested loops" column
(the one the OP is talking about) I can only see Java being faster than
C# - and in some cases the Java is faster than the C++.


The next to last column on the right of all three tables. On this column
on these tables MS C++ won all but one the fifteen benchmarks.
On this column of these tables C++ consistently beat C# by a factor
of about 500%.
Also note that the released Java 1.5 isn't part of the results, and
neither is the IBM JVM (which I believe would be a lot better at the
trig - I don't know about the other results).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #11

P: n/a

"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message news:%2****************@TK2MSFTNGP15.phx.gbl...

"Peter Olcott" <ol****@att.net> wrote in message news:RQNPe.2261$UI.1020@okepread05...
http://www.tommti-systems.de/go.html...enchmarks.html
Why is C# 500% slower than C++ on Nested Loops ???
Will this problem be solved in the future???

In general, such kind of micro-benchmarks have little or no value, in this particular case, it shows that the C# language compiler
could have done a better job in optimizing the IL.
To prove it, here are the results of the nested loop run comparing C++, C++/CLI and C#.

//C++ native (Compiler version 13.00)
Nested Loop elapsed time: 6048 ms -1804337152
//C++ managed - compiler options /clr /O2 /EHsc (14.00)
Nested Loop elapsed time: 6047 ms -1804337152
//C#
v1.1
Nested Loop elapsed time: 19800 ms - -1804337152
v2.0
Nested Loop elapsed time: 21050 ms - -1804337152

See, the native C++ and managed C++ results are the same, this indicates that it's not a JIT issue but an IL issue.
When comparing the generated IL of both C# and C++/CLI, it's the C++/CLI language compiler who did a better job than C#. However,
for the


That is very helpful information. That provides me with just
the sort of answer that I was looking for. I wanted to know
whether or not there is any inherent property of the .NET
architecture that prevented it from executing the fastest possible
code. Your little experiment would seem to prove that the
answer is no.
'Exceptions" case, it's C# who is the clear winner (managed code), so From the tables on the link that I provided, C# was consistently
400% slower than C++.
suppose this is the result of the differences in priorities and constraints set by the different language teams.
Willy.

Nov 17 '05 #12

P: n/a

"Peter Olcott" <ol****@att.net> wrote in message
news:7qYPe.2271$UI.450@okepread05...

"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...

"Peter Olcott" <ol****@att.net> wrote in message
news:RQNPe.2261$UI.1020@okepread05...
http://www.tommti-systems.de/go.html...enchmarks.html
'Exceptions" case, it's C# who is the clear winner (managed code), so

From the tables on the link that I provided, C# was consistently
400% slower than C++.


I was comparing C# and C++/CLI (both managed code) where C# is twice as fast
as C++/CLI, NOT C# and native C++!

Willy.
Nov 17 '05 #13

P: n/a

"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message news:Ol****************@TK2MSFTNGP10.phx.gbl...
'Exceptions" case, it's C# who is the clear winner (managed code), so From the tables on the link that I provided, C# was consistently
400% slower than C++.


I was comparing C# and C++/CLI (both managed code) where C# is twice as fast as C++/CLI, NOT C# and native C++!


Can you post these results please?
Thanks again for your help. The first answer that you
provided has proven to me the .NET development will
eventually replace all development on the Windows platform.
..NET is a superb Rapid Application Development architecture.


Willy.

Nov 17 '05 #14

P: n/a

"Peter Olcott" <ol****@att.net> wrote in message
news:WO_Pe.2276$UI.751@okepread05...

"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
news:Ol****************@TK2MSFTNGP10.phx.gbl...
'Exceptions" case, it's C# who is the clear winner (managed code), so
From the tables on the link that I provided, C# was consistently
400% slower than C++.

I was comparing C# and C++/CLI (both managed code) where C# is twice as
fast as C++/CLI, NOT C# and native C++!


Can you post these results please?
Thanks again for your help. The first answer that you
provided has proven to me the .NET development will
eventually replace all development on the Windows platform.
.NET is a superb Rapid Application Development architecture.


Why would you need those results to prove that to you?
If the code inside was actually doing anything - dealing with a database or
the GUI or any other I/O, responding to events, whatever, then you'd have a
hard time even measuring any performance penalty. (You'd probably find the
C# faster than native C++, if both were coded ideomatically - and you'd
certainly find the C# easier to write and read and be free of most memory
issues.) If you're trying to predict the weather or code the inner loop of
your renderer, then you're using the wrong tool and you should code that 1%
of your program (after profiling) in native code.

Even so, it's rarely the technical merits alone that decide if a language
will be a success.

m


Willy.


Nov 17 '05 #15

P: n/a

"Mike" <vi********@yahoo.com> wrote in message news:%2****************@tk2msftngp13.phx.gbl...

"Peter Olcott" <ol****@att.net> wrote in message news:WO_Pe.2276$UI.751@okepread05...

"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message news:Ol****************@TK2MSFTNGP10.phx.gbl...
> 'Exceptions" case, it's C# who is the clear winner (managed code), so
From the tables on the link that I provided, C# was consistently
400% slower than C++.
I was comparing C# and C++/CLI (both managed code) where C# is twice as fast as C++/CLI, NOT C# and native C++!
Can you post these results please?
Thanks again for your help. The first answer that you
provided has proven to me the .NET development will
eventually replace all development on the Windows platform.
.NET is a superb Rapid Application Development architecture.


Why would you need those results to prove that to you?


You have generally shown that the performance of code is dependent upon the
code generator, and optimizer rather than any inherent features of the .NET
architecture. The one exception to this seems to be "exceptions." I want
to fully understand why this might be.
If the code inside was actually doing anything - dealing with a database or the GUI or any other I/O, responding to events,
whatever, then you'd have a hard time even measuring any performance penalty. (You'd probably find the
I have developed a system that I am planning on porting the C# .NET
architecture. It is a system rather than application program. Its performance
requirements are based on real-time response rate. It is very computationally
intensive. Far greater than 99% of its time is spent performing computations.
From the results that you provided, in the worst case scenario I can
switch to C++ .NET from C# .NET, to achieve my required performance.
I want to completely verify this before I spend too much time learning .NET
C# faster than native C++, if both were coded ideomatically - and you'd certainly find the C# easier to write and read and be free
of most memory issues.) If you're trying to predict the weather or code the inner loop of your renderer, then you're using the
wrong tool and you should code that 1% of your program (after profiling) in native code.

Even so, it's rarely the technical merits alone that decide if a language will be a success.

m


Willy.



Nov 17 '05 #16

P: n/a
Peter Olcott <ol****@att.net> wrote:
Bob Grommes <bo*@bobgrommes.com> wrote:
Well ... you will note it's more than 3 times faster than Java, which is
a much more appropriate comparison.


Which result are you looking at? Looking at the "nested loops" column
(the one the OP is talking about) I can only see Java being faster than
C# - and in some cases the Java is faster than the C++.


The next to last column on the right of all three tables. On this column
on these tables MS C++ won all but one the fifteen benchmarks.
On this column of these tables C++ consistently beat C# by a factor
of about 500%.


But C# doesn't beat Java there, which Bob said it did. (Unless he meant
that C++ was still beating Java, in which case the comment was accurate
but misleading - I don't see why comparing Java and C++ is more
appropriate than comparing C# and C++.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #17

P: n/a
updated results & benchmarks:

http://www.cuj.com/documents/s=9816/...kschlegel.html

Thomas
Nov 17 '05 #18

This discussion thread is closed

Replies have been disabled for this discussion.