it's my first attempt to produce a native c++ code, then calling functions from c#, to test the overhead i was hearing about when interoping /invoking code. the test is simple math calculation, first a single run, and then 10K iterations in a loop.
class Program
{
[DllImport("ExportTest.dll", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
public static extern void doWarmUp();
[DllImport("ExportTest.dll", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
public static extern int doOne();
[DllImport("ExportTest.dll", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
public static extern int doLongOne();
static void Main(string[] args)
{
doWarmUp();
Stopwatch sw = new Stopwatch();
int test;
sw.Reset();
sw.Start();
test = csdoOne();
sw.Stop();
Console.WriteLine("Res CsdoOne:{0} ", sw.ElapsedTicks);
sw.Reset();
sw.Start();
test = doOne();
sw.Stop();
Console.WriteLine("Res C++ doOne :{0} ", sw.ElapsedTicks);
sw.Reset();
sw.Start();
test = doLongOne();
sw.Stop();
Console.WriteLine("Res C++ doLongOne:{0}\tTics: {1} ", test, sw.ElapsedTicks);
sw.Reset();
sw.Start();
test = csdoLongOne();
sw.Stop();
Console.WriteLine("Res C# doLongOne:{0}\tTics: {1} ", test, sw.ElapsedTicks);
Console.Read();
}
static int csdoOne()
{
int res;
res = 5 * 4;
return res;
}
static int csdoLongOne()
{
int r1, r2;
r1 = 0; r2 = 0;
for (int i = 0; i < 10500; i++)
{
r1 = (5 * 4);
r2 = i * 2;
r1 += r2;
}
return r2;
}
}
if it is interesting to see the c++ code i will post it too,
i did not do it cause it is actually the exact same code in both function
the warm-up function is just void doWarmUp(){ return;}
, as i have notes that only first call has any overhead.
the results as plotted are astonishing as i really like c#
Res Cs doOne: 4
Res C++ doOne: 17
Res c++ doLongOne: 21018 Tics: 30
Res C# doLongOne: 21018 Tics: 446
it's only simple math... am i doing anything wrong ?