You are seeing the effect of the C# compilers less-aggressive
optimisizer (compared to the C++'s compiler) --- when given REALLY DUMB
CODE
to work on.
To demostrate, VC# 2003, release build, reduces that code to
for (int n= 0; n!= 6000000; n++)
{
int num2 = ((n% 5) * 2) / 3;
double num3 = ((((double) (n* 3)) / 3.5) * 2) / 3;
int num4 = ((n/ 2) * 2) / 3;
}
On the other hand : VC++ 2003, release build, reduces that code to:
That's right. C++ completely eliminates the limit, as nothing that
happens
inside the loop, has any effect on anything outside the loop. The 700ms
you see is probably just the startup code.
Change your test to:
double a = 0,b = 0, c = 0, d = 0, e = 0;
double total = 0;
for(int n = 0; n != 6000000; n++)
{
a = n % 5 *2 / 3 - 4 + 6 / 3 - n + n * 2;
b = n * 2.3 - 1 *2 / 3 - 4 + 6 / 3 - n + n * 2;
c = n * 3 / 3.5 *2 / 3 - 4 + 6 / 3 - n + n * 2;
d = n + 1 * 2 *2 / 3 - 4 + 6 / 3 - n + n * 2;
e = n / 2 *2 / 3 - 4 + 6 / 3 - n + n * 2;
total += a + b + c + d + e;
}
Console.WriteLine("total = {0}", total);
And you'll probably get very different (from your eariler results) and
very
similar (among the different languages) results.
Well, actually, The C++ will still win by a lot, because the optimizer is
still able to reduce the code to:
public static int modopt(CallConvCdecl) main()
{
double num5 = 0;
int num1 = 0;
int num3 = 0;
int num2 = 0;
do
{
double num4 = num1;
double num6 = num3;
num5 = (((((((((num2 * 0.19047619047619047) - 4) + 2) - num4) +
num6) + (((((num4 * 2.3) - 4) + 2) - num4) + num6)) + ((num1 + (((num1 %
5)
<< 1) / 3)) - 2)) + ((num1 + (((num1 / 2) << 1) / 3)) - 2)) + (num3 - 1))
+
num5;
num1++;
num2 += 3;
num3 += 2;
}
while (num2 != 18000000);
Console.WriteLine(num5);
return 0;
Pretty cool huh?
But still, we aren't test either compilers abilty to perform math
operations, as mush as we're testing their abilities to optimize REALLY
DUMB
CODE. Stick a real world example into your test harness and see what
results you get.
--
--
Truth,
James Curran
[erstwhile VC++ MVP]
Home:
www.noveltheory.com Work:
www.njtheater.com
Blog:
www.honestillusion.com Day Job:
www.partsearch.com
Rich said:
I was considering C# for developing a scientific application, but I have
noticed a ~30% difference between VC++ .NET and C# on the same machine,
under identical conditions:
double a = 0,b = 0, c = 0, d = 0, e = 0;
for(int n = 0; n != 6000000; n++)
{
a = n % 5 *2 / 3 - 4 + 6 / 3 - n + n * 2;
b = n * 2.3 - 1 *2 / 3 - 4 + 6 / 3 - n + n * 2;
c = n * 3 / 3.5 *2 / 3 - 4 + 6 / 3 - n + n * 2;
d = n + 1 * 2 *2 / 3 - 4 + 6 / 3 - n + n * 2;
e = n / 2 *2 / 3 - 4 + 6 / 3 - n + n * 2;
}
It takes 2300ms in VB.NET, 1000ms in C# but only 700ms in VC++
Does this sound right?
Thanks.
Richard