Why is C# 450% slower than C++ on nested loops ??

P

Peter Olcott

Nicholas Paldino said:
For those that would like more information on why this question is being
asked, see the following link:

http://groups.google.com/group/micr...r+author:Olcott&rnum=1&hl=en#ebcc573d00467eec

So then it seems that your contention (based on the message that the link points
to) is that the whole difference is because of poorer code generation by the C#
compiler.
A good amount of the reply, and the OPs misunderstanding of the nature of
.NET and the CLR is in this thread.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Peter Olcott said:
http://www.tommti-systems.de/go.htm...ain-Dateien/reviews/languages/benchmarks.html
The above link shows that C# is 450% slower on something as simple as a
nested loop. Is this because .NET is inherently slower or does the C#
compiler merely produce code that is not as well optimized as the C++
compiler?
 
N

Nicholas Paldino [.NET/C# MVP]

Not at all. I never said .NET was slower than C++ code, nor did I say
that if it was slower, it was because C# compiles in a more inefficent
manner.

The thing you don't get is that IL runs through the CLR, which
transforms it into native code that is then run. End of story. Until you
accept that, none of the benchmarks or claims that you make will be valid.
Especially if you don't post the code in question which you claim is:

- 100 lines
- patent pending
- part of a system which is worth millions of dollars

You seem to have a history of long, intentionally confrontational posts
and we don't need another one.

Now I'm really done feeding the troll.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Peter Olcott said:
Nicholas Paldino said:
For those that would like more information on why this question is
being asked, see the following link:

http://groups.google.com/group/micr...r+author:Olcott&rnum=1&hl=en#ebcc573d00467eec

So then it seems that your contention (based on the message that the link
points to) is that the whole difference is because of poorer code
generation by the C# compiler.
A good amount of the reply, and the OPs misunderstanding of the nature
of .NET and the CLR is in this thread.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Peter Olcott said:
http://www.tommti-systems.de/go.htm...ain-Dateien/reviews/languages/benchmarks.html
The above link shows that C# is 450% slower on something as simple as a
nested loop. Is this because .NET is inherently slower or does the C#
compiler merely produce code that is not as well optimized as the C++
compiler?
 
M

Michael Bray

- part of a system which is worth millions of dollars

The part I don't get is that the money-men are going to let a little thing
like "must be written in .NET" get between them and their millions. :)

-mdb
 
N

Nicholas Paldino [.NET/C# MVP]

Michael,

I guess that's why I don't have millions of dollars already! =)
 
P

Peter Olcott

Nicholas Paldino said:
Not at all. I never said .NET was slower than C++ code, nor did I say that
if it was slower, it was because C# compiles in a more inefficent manner.

If you look at what I said, you will see that you just agreed with what I said.
The thing you don't get is that IL runs through the CLR, which transforms
it into native code that is then run. End of story. Until you accept that,
none of the benchmarks or claims that you make will be valid. Especially if
you don't post the code in question which you claim is:

- 100 lines
- patent pending
- part of a system which is worth millions of dollars

You seem to have a history of long, intentionally confrontational posts and
we don't need another one.

Now I'm really done feeding the troll.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Peter Olcott said:
Nicholas Paldino said:
For those that would like more information on why this question is being
asked, see the following link:

http://groups.google.com/group/micr...r+author:Olcott&rnum=1&hl=en#ebcc573d00467eec

So then it seems that your contention (based on the message that the link
points to) is that the whole difference is because of poorer code generation
by the C# compiler.
A good amount of the reply, and the OPs misunderstanding of the nature of
.NET and the CLR is in this thread.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


http://www.tommti-systems.de/go.htm...ain-Dateien/reviews/languages/benchmarks.html
The above link shows that C# is 450% slower on something as simple as a
nested loop. Is this because .NET is inherently slower or does the C#
compiler merely produce code that is not as well optimized as the C++
compiler?
 
N

Nicholas Paldino [.NET/C# MVP]

Joanna,

Which one, alt.inventors? That's a non-tech group I saw...
 
P

Peter Olcott

Michael Bray said:
The part I don't get is that the money-men are going to let a little thing
like "must be written in .NET" get between them and their millions. :)

If it can be written in .NET without any loss of speed it might have greater
value. It would seem to me that non managed code will be eventually phased out,
at least on the MS Windows platform.
 
P

Peter Olcott

Nicholas Paldino said:
Michael,

I guess that's why I don't have millions of dollars already! =)
I have about 30,000 hours invested in various inventions since 1987. This
invention was the first one to receive very strong patent protection. It would
seem that I did a good job on writing the patent for three reasons:
(1) It scored very high on all of the http://www.patentratings.com/ measures.
(2) It passed scrutiny by the USPTO the first time without reservation.
(3) The six independent claims provide very broad protection. The functional
results that I achieve can not be achieved without directly reading on these
claims.

The functional results that I achieve make possible great simplification to the
ease of use of graphical user interface based operating systems.
My original patent attorney graduated number one in his law school class of 144
graduates.
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Michael Bray said:
The part I don't get is that the money-men are going to let a little thing
like "must be written in .NET" get between them and their millions. :)

-mdb
 
M

Marcus Cuda

OK, momma said not to feed the trolls, but I was curious.

This is a silly benchmark, since it is basically calculating n^6. The C++ and C++/CLI compiler figures this out, but the
C# compiler doesn't. Interestingly, neither does the JITer. Everything was compiled with optimizations on under VS2005.

The loop:
for (a=0; a<n; a++)
for (b=0; b<n; b++)
for (c=0; c<n; c++)
for (d=0; d<n; d++)
for (e=0; e<n; e++)
for (f=0; f<n; f++)
x++;

C++ assembly:
00401025 mov ecx,eax
00401027 imul ecx,eax
0040102A imul ecx,eax
0040102D imul ecx,eax
00401030 imul ecx,eax
00401033 imul ecx,eax


C++/CLI IL:
L_000f: ldloc.0
L_0010: dup
L_0011: mul
L_0012: ldloc.0
L_0013: mul
L_0014: ldloc.0
L_0015: mul
L_0016: ldloc.0
L_0017: mul
L_0018: ldloc.0
L_0019: mul

C++/CLI JITed:
00000046 mov edi,esi
00000048 mov eax,esi
0000004a mov edx,esi
0000004c mov ecx,esi
0000004e mov ebx,esi
00000050 imul ebx,esi
00000053 imul ecx,ebx
00000056 imul edx,ecx
00000059 imul eax,edx
0000005c imul edi,eax


C# IL:
L_0000: ldc.i4.0
L_0001: stloc.s num7
L_0003: ldc.i4.0
L_0004: stloc.0
L_0005: br.s L_0055
L_0007: ldc.i4.0
L_0008: stloc.1
L_0009: br.s L_004d
L_000b: ldc.i4.0
L_000c: stloc.2
L_000d: br.s L_0045
L_000f: ldc.i4.0
L_0010: stloc.3
L_0011: br.s L_003d
L_0013: ldc.i4.0
L_0014: stloc.s num5
L_0016: br.s L_0034
L_0018: ldc.i4.0
L_0019: stloc.s num6
L_001b: br.s L_0029
L_001d: ldloc.s num7
L_001f: ldc.i4.1
L_0020: add
L_0021: stloc.s num7
L_0023: ldloc.s num6
L_0025: ldc.i4.1
L_0026: add
L_0027: stloc.s num6
L_0029: ldloc.s num6
L_002b: ldarg.0
L_002c: blt.s L_001d
L_002e: ldloc.s num5
L_0030: ldc.i4.1
L_0031: add
L_0032: stloc.s num5
L_0034: ldloc.s num5
L_0036: ldarg.0
L_0037: blt.s L_0018
L_0039: ldloc.3
L_003a: ldc.i4.1
L_003b: add
L_003c: stloc.3
L_003d: ldloc.3
L_003e: ldarg.0
L_003f: blt.s L_0013
L_0041: ldloc.2
L_0042: ldc.i4.1
L_0043: add
L_0044: stloc.2
L_0045: ldloc.2
L_0046: ldarg.0
L_0047: blt.s L_000f
L_0049: ldloc.1
L_004a: ldc.i4.1
L_004b: add
L_004c: stloc.1
L_004d: ldloc.1
L_004e: ldarg.0
L_004f: blt.s L_000b
L_0051: ldloc.0
L_0052: ldc.i4.1
L_0053: add
L_0054: stloc.0
L_0055: ldloc.0
L_0056: ldarg.0
L_0057: blt.s L_0007


C# JITed:
0000003e xor edx,edx
00000040 mov dword ptr [esp],edx
00000043 nop
00000044 jmp 0000009A
00000046 xor edx,edx
00000048 mov dword ptr [esp+4],edx
0000004c nop
0000004d jmp 00000091
0000004f xor ebx,ebx
00000051 nop
00000052 jmp 00000089
00000054 xor edx,edx
00000056 mov dword ptr [esp+8],edx
0000005a nop
0000005b jmp 00000082
0000005d xor edx,edx
0000005f mov dword ptr [esp+0Ch],edx
00000063 nop
00000064 jmp 00000078
00000066 xor ebp,ebp
00000068 nop
00000069 jmp 00000070
0000006b inc dword ptr [esp+10h]
0000006f inc ebp
00000070 cmp ebp,esi
00000072 jl 0000006B
00000074 inc dword ptr [esp+0Ch]
00000078 cmp dword ptr [esp+0Ch],esi
0000007c jl 00000066
0000007e inc dword ptr [esp+8]
00000082 cmp dword ptr [esp+8],esi
00000086 jl 0000005D
00000088 inc ebx
00000089 cmp ebx,esi
0000008b jl 00000054
0000008d inc dword ptr [esp+4]
00000091 cmp dword ptr [esp+4],esi
00000095 jl 0000004F
00000097 inc dword ptr [esp]
0000009a cmp dword ptr [esp],esi
0000009d jl 00000046
 
N

Nicholas Paldino [.NET/C# MVP]

Seriously, stop, you are embarassing yourself.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Peter Olcott said:
Nicholas Paldino said:
Michael,

I guess that's why I don't have millions of dollars already! =)
I have about 30,000 hours invested in various inventions since 1987. This
invention was the first one to receive very strong patent protection. It
would seem that I did a good job on writing the patent for three reasons:
(1) It scored very high on all of the http://www.patentratings.com/
measures.
(2) It passed scrutiny by the USPTO the first time without reservation.
(3) The six independent claims provide very broad protection. The
functional results that I achieve can not be achieved without directly
reading on these claims.

The functional results that I achieve make possible great simplification
to the ease of use of graphical user interface based operating systems.
My original patent attorney graduated number one in his law school class
of 144 graduates.
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Michael Bray said:
in

- part of a system which is worth millions of dollars


The part I don't get is that the money-men are going to let a little
thing
like "must be written in .NET" get between them and their millions. :)

-mdb
 
P

Peter Olcott

Nicholas Paldino said:
Joanna,

Which one, alt.inventors? That's a non-tech group I saw...

misc.int-property is much more useful. There are real patent agents and at least
one patent attorney that provide good advice for free. It can be verified as
good advice by cross validation.
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Joanna Carter said:
"Nicholas Paldino [.NET/C# MVP]" <[email protected]> a écrit
dans le message de eFs0Pb%[email protected]...

| Now I'm really done feeding the troll.

Heh, you ought to come and see what we have had to deal with in our non-tech
group :))

Joanna
 
P

Peter Olcott

Marcus Cuda said:
OK, momma said not to feed the trolls, but I was curious.

This is a silly benchmark, since it is basically calculating n^6. The C++ and
C++/CLI compiler figures this out, but the C# compiler doesn't. Interestingly,
neither does the JITer. Everything was compiled with optimizations on under
VS2005.

The loop:
for (a=0; a<n; a++)
for (b=0; b<n; b++)
for (c=0; c<n; c++)
for (d=0; d<n; d++)
for (e=0; e<n; e++)
for (f=0; f<n; f++)
x++;

for (a=0; a<n; a++)
for (b=0; b<n; b++)
for (c=0; c<n; c++)
for (d=0; d<n; d++)
for (e=0; e<n; e++)
for (f=0; f<n; f++)
x+=a+b+c+d+e+f;

If you don't copy the code the way that it was posted, then the optimizer might
optimize away the loops. The whole point is to see how fast the looping is.

C++ assembly:
00401025 mov ecx,eax
00401027 imul ecx,eax
0040102A imul ecx,eax
0040102D imul ecx,eax
00401030 imul ecx,eax
00401033 imul ecx,eax


C++/CLI IL:
L_000f: ldloc.0
L_0010: dup
L_0011: mul
L_0012: ldloc.0
L_0013: mul
L_0014: ldloc.0
L_0015: mul
L_0016: ldloc.0
L_0017: mul
L_0018: ldloc.0
L_0019: mul

C++/CLI JITed:
00000046 mov edi,esi
00000048 mov eax,esi
0000004a mov edx,esi
0000004c mov ecx,esi
0000004e mov ebx,esi
00000050 imul ebx,esi
00000053 imul ecx,ebx
00000056 imul edx,ecx
00000059 imul eax,edx
0000005c imul edi,eax


C# IL:
L_0000: ldc.i4.0
L_0001: stloc.s num7
L_0003: ldc.i4.0
L_0004: stloc.0
L_0005: br.s L_0055
L_0007: ldc.i4.0
L_0008: stloc.1
L_0009: br.s L_004d
L_000b: ldc.i4.0
L_000c: stloc.2
L_000d: br.s L_0045
L_000f: ldc.i4.0
L_0010: stloc.3
L_0011: br.s L_003d
L_0013: ldc.i4.0
L_0014: stloc.s num5
L_0016: br.s L_0034
L_0018: ldc.i4.0
L_0019: stloc.s num6
L_001b: br.s L_0029
L_001d: ldloc.s num7
L_001f: ldc.i4.1
L_0020: add
L_0021: stloc.s num7
L_0023: ldloc.s num6
L_0025: ldc.i4.1
L_0026: add
L_0027: stloc.s num6
L_0029: ldloc.s num6
L_002b: ldarg.0
L_002c: blt.s L_001d
L_002e: ldloc.s num5
L_0030: ldc.i4.1
L_0031: add
L_0032: stloc.s num5
L_0034: ldloc.s num5
L_0036: ldarg.0
L_0037: blt.s L_0018
L_0039: ldloc.3
L_003a: ldc.i4.1
L_003b: add
L_003c: stloc.3
L_003d: ldloc.3
L_003e: ldarg.0
L_003f: blt.s L_0013
L_0041: ldloc.2
L_0042: ldc.i4.1
L_0043: add
L_0044: stloc.2
L_0045: ldloc.2
L_0046: ldarg.0
L_0047: blt.s L_000f
L_0049: ldloc.1
L_004a: ldc.i4.1
L_004b: add
L_004c: stloc.1
L_004d: ldloc.1
L_004e: ldarg.0
L_004f: blt.s L_000b
L_0051: ldloc.0
L_0052: ldc.i4.1
L_0053: add
L_0054: stloc.0
L_0055: ldloc.0
L_0056: ldarg.0
L_0057: blt.s L_0007


C# JITed:
0000003e xor edx,edx
00000040 mov dword ptr [esp],edx
00000043 nop
00000044 jmp 0000009A
00000046 xor edx,edx
00000048 mov dword ptr [esp+4],edx
0000004c nop
0000004d jmp 00000091
0000004f xor ebx,ebx
00000051 nop
00000052 jmp 00000089
00000054 xor edx,edx
00000056 mov dword ptr [esp+8],edx
0000005a nop
0000005b jmp 00000082
0000005d xor edx,edx
0000005f mov dword ptr [esp+0Ch],edx
00000063 nop
00000064 jmp 00000078
00000066 xor ebp,ebp
00000068 nop
00000069 jmp 00000070
0000006b inc dword ptr [esp+10h]
0000006f inc ebp
00000070 cmp ebp,esi
00000072 jl 0000006B
00000074 inc dword ptr [esp+0Ch]
00000078 cmp dword ptr [esp+0Ch],esi
0000007c jl 00000066
0000007e inc dword ptr [esp+8]
00000082 cmp dword ptr [esp+8],esi
00000086 jl 0000005D
00000088 inc ebx
00000089 cmp ebx,esi
0000008b jl 00000054
0000008d inc dword ptr [esp+4]
00000091 cmp dword ptr [esp+4],esi
00000095 jl 0000004F
00000097 inc dword ptr [esp]
0000009a cmp dword ptr [esp],esi
0000009d jl 00000046
 
P

Peter Olcott

Nicholas Paldino said:
Seriously, stop, you are embarassing yourself.

Time will tell.
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Peter Olcott said:
Nicholas Paldino said:
Michael,

I guess that's why I don't have millions of dollars already! =)
I have about 30,000 hours invested in various inventions since 1987. This
invention was the first one to receive very strong patent protection. It
would seem that I did a good job on writing the patent for three reasons:
(1) It scored very high on all of the http://www.patentratings.com/ measures.
(2) It passed scrutiny by the USPTO the first time without reservation.
(3) The six independent claims provide very broad protection. The functional
results that I achieve can not be achieved without directly reading on these
claims.

The functional results that I achieve make possible great simplification to
the ease of use of graphical user interface based operating systems.
My original patent attorney graduated number one in his law school class of
144 graduates.
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


- part of a system which is worth millions of dollars


The part I don't get is that the money-men are going to let a little thing
like "must be written in .NET" get between them and their millions. :)

-mdb
 
C

Cecil

I'm not a compiler authority, but I did believe it common knowledge
that the C++ compiler was the best of the bunch in terms on speed. The
engineers behind it have said that even managed code done in C++ will
outperform the other languages.

I doubt 450% is likely though. It sounds to me like you may be
comparing binary code to code that has yet to be compiled which
obviously will run signifigantly slower in most instances.
 
M

Marcus Cuda

If you don't copy the code the way that it was posted, then the optimizer might
optimize away the loops. The whole point is to see how fast the looping is.
my apologizes. I didn't see the code listed on the site you gave (I do now), so I grabbed the code from
http://dada.perl.it/shootout - which was linked from your url.
 
P

Peter Olcott

Cecil said:
I'm not a compiler authority, but I did believe it common knowledge
that the C++ compiler was the best of the bunch in terms on speed. The
engineers behind it have said that even managed code done in C++ will
outperform the other languages.

I doubt 450% is likely though. It sounds to me like you may be
comparing binary code to code that has yet to be compiled which
obviously will run signifigantly slower in most instances.

http://www.tommti-systems.de/go.htm...Dateien/reviews/languages/benchmarks.htmlHere are the benchmarks. 24,586 millisencons to 5,438 milliseconds. It is notclear whether or not the C++ is managed C++ or native code.
 

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