其他分享
首页 > 其他分享> > 为什么本地c与c interop表现不佳?

为什么本地c与c interop表现不佳?

作者:互联网

我在下面发布了一些代码来测试使用Visual Studio 2010从c / cli本机c和c#调用方法的性能(以毫秒为单位).我有一个单独的本机c项目,它被编译成dll.当我从c调用c时,我得到的预期结果比管理对应的快得多(大约4倍).但是,当我从c / cli调用c时,性能慢了10倍.

从c / cli调用本机c时这是一种预期的行为吗?我的印象是不应该有显着差异,但这个简单的测试显示不然.这可能是c和c / cli编译器之间的优化差异吗?

更新

我对cpp进行了一些更新,因此我没有在紧密的循环中调用方法(正如Reed Copsey所指出的那样),并且结果表明性能上的差异无关紧要或非常小.当然,取决于互操作的方式.

.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.

桥接CLR和本机代码需要编组.从C/C++LI进入本机方法调用时,每个方法调用总会有一些开销.

开销(在这种情况下)看起来如此之大的唯​​一原因是你在紧密的循环中调用一个非常快的方法.如果您要批处理该类,或者调用一个在运行时方面明显更长的方法,您会发现开销非常小.

标签:c,visual-studio-2010,c-cli
来源: https://codeday.me/bug/20190831/1776189.html