Why does native c ++ work badly with c ++ interop?

advertisements

I have posted some code below to test the performance (time-wise in milliseconds) of calling a method from native c++ and c# from c++/cli using Visual Studio 2010. I have a separate native c++ project which is compiled into dll. When I call into c++ from c++, I get the expected result which is much faster (about 4x) than the managed counterparts. However, when I call into c++ from c++/cli, the performance is 10x slower.

Is this an expected behavior when calling into native c++ from c++/cli? I was under the impression that there shouldn't be a significant difference, but this simple test is showing otherwise. Could this be an optimization difference between the c++ and c++/cli compiler?

Update

I made some update to the cpp, so that I'm not calling a method in a tight loop (as Reed Copsey pointed out), and it turns out that the difference in performance in insignificant or very small. Depending on how the inter-operation is being done, of course.

.h

#ifndef CPPOBJECT_H
#define CPPOBJECT_H

#ifdef CPLUSPLUSOBJECT_EXPORTING
    #define CLASS_DECLSPEC __declspec(dllexport)
#else
    #define CLASS_DECLSPEC __declspec(dllimport)
#endif

class CLASS_DECLSPEC CPlusPlusObject
{
public:
    CPlusPlusObject(){}
    ~CPlusPlusObject(){}

    void sayHello();
    double getSqrt(double n);
    // Update
    double wasteSomeTimeWithSqrt(double n);
};

#endif

.cpp

#include "CPlusPlusObject.h"
#include <iostream>

void CPlusPlusObject::sayHello(){std::cout << "Hello";}
double CPlusPlusObject::getSqrt(double n) {return std::sqrt(n);}
double CPlusPlusObject::wasteSomeTimeWithSqrt(double n)
{
    double result = 0;
    for (int x = 0; x < 10000000; x++)
    {
        result += std::sqrt(n);
    }
    return result;
}

c++/cli

const unsigned set = 100;
const unsigned repetitions = 1000000;
double cppcliTocpp()
{
    double n = 0;
    System::Diagnostics::Stopwatch^ stopWatch = gcnew System::Diagnostics::Stopwatch();

     stopWatch->Start();
     while (stopWatch->ElapsedMilliseconds < 1200){n+=0.001;}
     stopWatch->Reset();

    for (int x = 0; x < set; x++)
    {
        stopWatch->Start();
        CPlusPlusObject cplusplusObject;
        n += cplusplusObject.wasteSomeTimeWithSqrt(123.456);
        /*for (int i = 0; i < repetitions; i++)
        {
            n += cplusplusObject.getSqrt(123.456);
        }*/
        stopWatch->Stop();
        System::Console::WriteLine("c++/cli call to native c++ took " + stopWatch->ElapsedMilliseconds + "ms.");
        stopWatch->Reset();
    }
    return n;
}

double cppcliTocSharp()
{
    double n = 0;
    System::Diagnostics::Stopwatch^ stopWatch = gcnew System::Diagnostics::Stopwatch();

    stopWatch->Start();
    while (stopWatch->ElapsedMilliseconds < 1200){n+=0.001;}
    stopWatch->Reset();

    for (int x = 0; x < set; x++)
    {
        stopWatch->Start();
        CSharp::CSharpObject^ cSharpObject = gcnew CSharp::CSharpObject();
        for (int i = 0; i < repetitions; i++)
        {
            n += cSharpObject->GetSqrt(123.456);
        }
        stopWatch->Stop();
        System::Console::WriteLine("c++/cli call to c# took " + stopWatch->ElapsedMilliseconds + "ms.");
        stopWatch->Reset();
    }
    return n;
}

double cppcli()
{
    double n = 0;
    System::Diagnostics::Stopwatch^ stopWatch = gcnew System::Diagnostics::Stopwatch();

    stopWatch->Start();
    while (stopWatch->ElapsedMilliseconds < 1200){n+=0.001;}
    stopWatch->Reset();

    for (int x = 0; x < set; x++)
    {
        stopWatch->Start();
        CPlusPlusCliObject cPlusPlusCliObject;
        for (int i = 0; i < repetitions; i++)
        {
            n += cPlusPlusCliObject.getSqrt(123.456);
        }
        stopWatch->Stop();
        System::Console::WriteLine("c++/cli took " + stopWatch->ElapsedMilliseconds + "ms.");
        stopWatch->Reset();
    }
    return n;
}

int main()
{
    double n = 0;
    n += cppcliTocpp();
    n += cppcliTocSharp();
    n += cppcli();
    System::Console::WriteLine(n);
    System::Console::ReadKey();
}


However, when I call into c++ from c++/cli, the performance is 10x slower.

Bridging the CLR and native code requires marshaling. There is always going to be some overhead in each method call when going from C++/CLI into a native method call.

The only reason the overhead (in this case) seems so large is that you're calling a very fast method in a tight loop. If you were to batch the class, or call a method that was significantly longer in terms of runtime, you'd find that the overhead is quite small.