On this page:
http://www.elitetrader.com/vb/showthread.php?s=&threadid=213861&perpage=6&pagenumber=10
there are 2 simple test (source code), just a couple of loops
the test seems to say that c++ is much faster than c#. Is this code
flawed or what ? I don't believe that is true ... what's the problem ?
I think the bug is with the guy that did the test.
while(true);
at the bottom of both programs. WTF?
Of all possible ways of preventing the console from closing when
running from GUI that must be the clumsiest.
I tried copying the code and running it here.
C:\>csc /o+ Z.cs
Microsoft (R) Visual C# 2010 Compiler version 4.0.30319.1
Copyright (C) Microsoft Corporation. All rights reserved.
Z.cs(38,13): warning CS0162: Unreachable code detected
C:\>Z
Milliseconds: 47
Milliseconds: 31
Milliseconds: 47
Milliseconds: 31
Milliseconds: 47
Milliseconds: 47
Milliseconds: 62
Milliseconds: 63
Milliseconds: 31
^C
C:\>cl /Ox /EHsc Z.cpp WinMM.lib
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01
for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
Z.cpp
Microsoft (R) Incremental Linker Version 10.00.30319.01
Copyright (C) Microsoft Corporation. All rights reserved.
/out:Z.exe
Z.obj
WinMM.lib
C:\>Z
Milliseconds: 20
Milliseconds: 19
Milliseconds: 19
Milliseconds: 19
Milliseconds: 19
Milliseconds: 20
Milliseconds: 18
Milliseconds: 19
Milliseconds: 18
^C
Not nearly the same difference as shown in the link, so
something must have been done that skewed the results
there.
But still a significant difference, but the C# numbers
does not seem stable - and it is not expected
to be either with so short intervals.
Let us change to have an outer loop that iterates 100 times.
C:\>csc /o+ Z.cs
Microsoft (R) Visual C# 2010 Compiler version 4.0.30319.1
Copyright (C) Microsoft Corporation. All rights reserved.
Z.cs(38,13): warning CS0162: Unreachable code detected
C:\>Z
Milliseconds: 3828
Milliseconds: 3813
Milliseconds: 3828
Milliseconds: 3812
Milliseconds: 3860
Milliseconds: 3812
Milliseconds: 3828
Milliseconds: 3813
Milliseconds: 3812
^C
C:\>cl /Ox /EHsc Z.cpp WinMM.lib
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01
for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
Z.cpp
Microsoft (R) Incremental Linker Version 10.00.30319.01
Copyright (C) Microsoft Corporation. All rights reserved.
/out:Z.exe
Z.obj
WinMM.lib
C:\>Z
Milliseconds: 1942
Milliseconds: 1945
Milliseconds: 1947
Milliseconds: 1947
Milliseconds: 1936
Milliseconds: 1964
Milliseconds: 1954
Milliseconds: 2105
Milliseconds: 1942
^C
Now they are both stable.
The difference is still higher than what would be expected.
But the operation inside the loops are very simple
meaning that the performance is very dependent on
how the optimizer handles a very specific case.
Let us add some more stuff.
for( int i=0;i< 30000000; i++)
{
x = x + param1;
x = x * 2;
x = x + 1;
}
for( int i=0;i< 30000000; i++)
{
x = x - 1;
x = x / 2;
x = x - param1;
}
and only doing 10 iterations over this.
C:\>csc /o+ Z.cs
Microsoft (R) Visual C# 2010 Compiler version 4.0.30319.1
Copyright (C) Microsoft Corporation. All rights reserved.
Z.cs(42,13): warning CS0162: Unreachable code detected
C:\>Z
Milliseconds: 1562
Milliseconds: 1547
Milliseconds: 1578
Milliseconds: 1563
Milliseconds: 1562
Milliseconds: 1579
Milliseconds: 1546
Milliseconds: 1563
Milliseconds: 1562
^C
C:\>cl /Ox /EHsc Z.cpp WinMM.lib
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01
for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
Z.cpp
Microsoft (R) Incremental Linker Version 10.00.30319.01
Copyright (C) Microsoft Corporation. All rights reserved.
/out:Z.exe
Z.obj
WinMM.lib
C:\>Z
Milliseconds: 959
Milliseconds: 969
Milliseconds: 970
Milliseconds: 962
Milliseconds: 952
Milliseconds: 953
Milliseconds: 954
Milliseconds: 949
Milliseconds: 957
^C
The difference became smaller, but is still significant.
If you add more different stuff then I would expect the
difference to become even smaller.
What can we conclude from that?
C++ is a lot faster than C# to execute the specific
code:
for( int i=0;i< 30000000; i++)
{
x = x + param1;
}
for( int i=0;i< 30000000; i++)
{
x = x - param1;
}
so if you ever want to code that code, then you should
use C++.
But maybe better hire a programmer that has spend
time learning to program and not spend time on
language wars. In which case the code would
look like:
x += 30000000*param1;
x -= 30000000*param1;
and that would be about a million times faster.
Also note that all results apply to specific software
versions on a specific CPU. Different software and
different CPU can create different results.
Arne