N
Nigel
I read that C#'s JIT compiler produces very efficient
machine code. However, I've found when performing
extensive numerical calculations that C# is less than a
fourth the speed of C++. I give code examples below. Both
C# and C++ were compiled as release builds with default
optimisation on (C++ has /Ob1 set in addition to default
optimizations). I suspected the relatively poor C#
performance was due to using managed memory, but
adding 'unsafe' to relevant classes and methods makes
little or no difference.
I'd appreciate advice on how I can bring the C#
performance close to that of C++, or an acknowledgement
that for the code samples below, C# is fundamentally
several times slower than (unmanaged) C++.
C++ Code:
double total = 0.0;
for (int rep = 0; rep < 5; rep++)
{
total /= 1000.0;
for (long i = 0; i < 100000000; i++)
{
total += i/999999.0;
double disc = total*total + i;
double root = (total + disc)/
(200000.0*(i + 1));
total -= root;
}
}
C# Code:
double total = 0.0;
for (int rep = 0; rep < 5; rep++)
{
total /= 1000.0;
for (long i = 0; i <
100000000; i++)
{
total +=
i/999999.0;
double disc =
total*total + i;
double root =
(total + disc)/(200000.0*(i + 1));
total -= root;
}
}
machine code. However, I've found when performing
extensive numerical calculations that C# is less than a
fourth the speed of C++. I give code examples below. Both
C# and C++ were compiled as release builds with default
optimisation on (C++ has /Ob1 set in addition to default
optimizations). I suspected the relatively poor C#
performance was due to using managed memory, but
adding 'unsafe' to relevant classes and methods makes
little or no difference.
I'd appreciate advice on how I can bring the C#
performance close to that of C++, or an acknowledgement
that for the code samples below, C# is fundamentally
several times slower than (unmanaged) C++.
C++ Code:
double total = 0.0;
for (int rep = 0; rep < 5; rep++)
{
total /= 1000.0;
for (long i = 0; i < 100000000; i++)
{
total += i/999999.0;
double disc = total*total + i;
double root = (total + disc)/
(200000.0*(i + 1));
total -= root;
}
}
C# Code:
double total = 0.0;
for (int rep = 0; rep < 5; rep++)
{
total /= 1000.0;
for (long i = 0; i <
100000000; i++)
{
total +=
i/999999.0;
double disc =
total*total + i;
double root =
(total + disc)/(200000.0*(i + 1));
total -= root;
}
}