Thanks for all the replies so far. I'm particularly interested in any
possible solutions or ways 'round this problem; the issue of whether
or not C# is interpreted etc I find somewhat confusing. The best
suggestion so far (assuming we continue with C# and don't just simply
write everything in C++ instead!) is to perform all our number
crunching in DLLs not written in C#.
Of course, there is still the issue of the correctness of our test.
For this I've listed the C# and C++ codes below. They both evaluate
the functions Tan, Sin, Cos, Exp, Log and simple multiplication each 1
million times. This is repeated 100 times, and the average time
required is calculated.
******************************************
The output from the test for C# is:
******************************************
PI 3.1415926535897900000000000
100.00 run avg 1,000,000.00 x tanges 219.86 ms
100.00 run avg 1,000,000.00 x sinus 15.00 ms
100.00 run avg 1,000,000.00 x cosinus 15.16 ms
100.00 run avg 1,000,000.00 x exp 253.61 ms
100.00 run avg 1,000,000.00 x log 180.64 ms
100.00 run avg 1,000,000.00 x theta * theta 15.94 ms
Total execution time 70.04 s
******************************************
The output from the test for C++ is:
******************************************
PI 3.1415926535897931000000000
100 run avg 1000000 x tanges 4.06 ms
100 run avg 1000000 x sinus 4.06 ms
100 run avg 1000000 x cosinus 4.06 ms
100 run avg 1000000 x exp 4.07 ms
100 run avg 1000000 x log 4.21 ms
100 run avg 1000000 x theta * theta 4.07 ms
Total execution time 2.45 s
******************************************
The C# program code
******************************************
using System;
namespace CSharpSpeed
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Speed
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main(string[] args)
{
DateTime start, end;
DateTime tmp1, tmp2;
double tmp;
TimeSpan time;
double pi = 2 * Math.Asin(1.0);
Console.Out.WriteLine("PI {0:F25}", pi);
start = DateTime.Now;
double twoPI = Math.PI * 2.0;
double stepCnt = 1000000.0;
double step = twoPI / stepCnt;
double theta;
double runs = 100;
int cntr;
time = new TimeSpan();
for (cntr = 0; cntr < runs; cntr++)
{
tmp1 = DateTime.Now;
for (theta = 0.0; theta < twoPI; theta += step)
{
tmp = Math.Tan(theta);
}
tmp2 = DateTime.Now;
time += tmp2 - tmp1;
}
tmp = time.TotalMilliseconds / runs;
Console.Out.WriteLine("{0:F} run avg {1:N} x tanges {2:F} ms",
runs, stepCnt, tmp);
time = new TimeSpan();
for (cntr = 0; cntr < runs; cntr++)
{
tmp1 = DateTime.Now;
for (theta = 0.0; theta < twoPI; theta += step)
{
tmp = Math.Sin(theta);
}
tmp2 = DateTime.Now;
time += tmp2 - tmp1;
}
tmp = time.TotalMilliseconds / runs;
Console.Out.WriteLine("{0:N} run avg {1:N} x sinus {2:F} ms",
runs, stepCnt, tmp);
time = new TimeSpan();
for (cntr = 0; cntr < runs; cntr++)
{
tmp1 = DateTime.Now;
for (theta = 0.0; theta < twoPI; theta += step)
{
tmp = Math.Cos(theta);
}
tmp2 = DateTime.Now;
time += tmp2 - tmp1;
}
tmp = time.TotalMilliseconds / runs;
Console.Out.WriteLine("{0:N} run avg {1:N} x cosinus {2:F} ms",
runs, stepCnt, tmp);
step = 2.0 / stepCnt;
time = new TimeSpan();
for (cntr = 0; cntr < runs; cntr++)
{
tmp1 = DateTime.Now;
for (theta = 0.0; theta < 2.0; theta += step)
{
tmp = Math.Exp(theta);
}
tmp2 = DateTime.Now;
time += tmp2 - tmp1;
}
tmp = time.TotalMilliseconds / runs;
Console.Out.WriteLine("{0:N} run avg {1:N} x exp {2:F} ms",
runs, stepCnt, tmp);
time = new TimeSpan();
for (cntr = 0; cntr < runs; cntr++)
{
tmp1 = DateTime.Now;
for (theta = step; theta < 2.0; theta += step)
{
tmp = Math.Log(theta);
}
tmp2 = DateTime.Now;
time += tmp2 - tmp1;
}
tmp = time.TotalMilliseconds / runs;
Console.Out.WriteLine("{0:N} run avg {1:N} x log {2:F} ms",
runs, stepCnt, tmp);
step = 1.0;
time = new TimeSpan();
for (cntr = 0; cntr < runs; cntr++)
{
tmp1 = DateTime.Now;
for (theta = 0.0; theta < stepCnt; theta += step)
{
tmp = theta * theta;
}
tmp2 = DateTime.Now;
time += tmp2 - tmp1;
}
tmp = time.TotalMilliseconds / runs;
Console.Out.WriteLine("{0:N} run avg {1:N} x theta * theta {2:F}
ms", runs, stepCnt, tmp);
end = DateTime.Now;
time = end - start;
Console.Out.WriteLine("Total execution time {0:F} s",
time.TotalSeconds);
Console.In.ReadLine();
}
}
}
******************************************
The C++ program code
******************************************
#define _USE_MATH_DEFINES
#include <iostream>
#include <time.h>
#include <math.h>
using namespace std;
int main(int argc, char* argv[])
{
clock_t start, end;
clock_t tmp1, tmp2;
double tmp;
double time;
double pi = 2 * asin(1.0);
printf("PI %25.25f\n", pi);
start = clock();
double twoPI = M_PI * 2.0;
double stepCnt = 1000000;
double step = twoPI / stepCnt;
double theta;
double runs = 100;
int cntr;
time = 0.0;
for (cntr = 0; cntr < runs; cntr++)
{
tmp1 = clock();
for (theta = 0.0; theta < twoPI; theta += step)
{
tmp = tan(theta);
}
tmp2 = clock();
time += tmp2 - tmp1;
}
time /= runs;
printf("%.0f run avg %.0f x tanges %.2f ms\n", runs, stepCnt, time);
time = 0.0;
for (cntr = 0; cntr < runs; cntr++)
{
tmp1 = clock();
for (theta = 0.0; theta < twoPI; theta += step)
{
tmp = sin(theta);
}
tmp2 = clock();
time += tmp2 - tmp1;
}
time /= runs;
printf("%.0f run avg %.0f x sinus %.2f ms\n", runs, stepCnt, time);
time = 0.0;
for (cntr = 0; cntr < runs; cntr++)
{
tmp1 = clock();
for (theta = 0.0; theta < twoPI; theta += step)
{
tmp = cos(theta);
}
tmp2 = clock();
time += tmp2 - tmp1;
}
time /= runs;
printf("%.0f run avg %.0f x cosinus %.2f ms\n", runs, stepCnt,
time);
step = 2.0 / stepCnt;
time = 0.0;
for (cntr = 0; cntr < runs; cntr++)
{
tmp1 = clock();
for (theta = 0.0; theta < 2.0; theta += step)
{
tmp = exp(theta);
}
tmp2 = clock();
time += tmp2 - tmp1;
}
time /= runs;
printf("%.0f run avg %.0f x exp %.2f ms\n", runs, stepCnt, time);
time = 0.0;
for (cntr = 0; cntr < runs; cntr++)
{
tmp1 = clock();
for (theta = step; theta < 2.0; theta += step)
{
tmp = log(theta);
}
tmp2 = clock();
time += tmp2 - tmp1;
}
time /= runs;
printf("%.0f run avg %.0f x log %.2f ms\n", runs, stepCnt, time);
step = 1.0;
time = 0.0;
for (cntr = 0; cntr < runs; cntr++)
{
tmp1 = clock();
for (theta = 0.0; theta < stepCnt; theta += step)
{
tmp = theta * theta;
}
tmp2 = clock();
time += tmp2 - tmp1;
}
time /= runs;
printf("%.0f run avg %.0f x theta * theta %.2f ms\n", runs, stepCnt,
time);
end = clock();
time = (end - start) / 1000.0;
printf("Total execution time %.2f s\n", time);
getchar();
return 0;
}