翻译非托管 C++托管 C++ 的代码用于从 C# 调用

发布于 2024-12-22 19:38:08 字数 644 浏览 2 评论 0原文

我有一些需要大量二进制操作的 C# 代码,因此我编写了一个非托管 C++ 方法来替换其中一个 C# 方法。令我震惊的是,速度慢了 10 倍。我运行了一个配置文件,发现速度缓慢来自调用外部方法的开销,而不是方法本身。

所以我想,如果我用托管 C++ 编写该方法,我将减少调用的开销,但仍然具有 C++ 的速度。首先,这个假设有效吗?

这是我的非托管 C++ 代码:

#include "stdafx.h";

unsigned __int32 _stdcall LSB_i32(unsigned __int32 x)
{
    DWORD result;
    _BitScanForward(&result, x);
    return (unsigned __int32)result;
}

这是我的 C# 代码:

public static partial class Binary
{
    [DllImport(@"CPP.dll")]
    public static extern int LSB_i32(int value);
}

我在这里做错了什么吗?

如何将上述内容转换为托管 C++?我对此进行了一些浏览,但由于我不熟悉托管 C++,所以没有走得太远。

I have some C# code that requires extensive binary manipulation, so I wrote an unmanaged C++ method to replace one of the C# methods. To my shock, is was 10 X slower. I ran a profile, and discovered the slowness comes from the overhead of calling the external method, not the method itself.

So I thought that if I write the method in managed C++, I will loose the overhead of the call, but still have the speed of C++. First, is this assumption valid?

Here is my unmanaged C++ code:

#include "stdafx.h";

unsigned __int32 _stdcall LSB_i32(unsigned __int32 x)
{
    DWORD result;
    _BitScanForward(&result, x);
    return (unsigned __int32)result;
}

Here is my C# code:

public static partial class Binary
{
    [DllImport(@"CPP.dll")]
    public static extern int LSB_i32(int value);
}

Am I doing anything wrong here?

How do I translate the above to managed C++? I did some browsing on this, but because I am unfamiliar with managed C++, I didn't get far.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

〗斷ホ乔殘χμё〖 2024-12-29 19:38:08

您可以保留非托管方法,但不应经常从托管代码中调用它。例如,如果您在紧密循环中调用非托管方法,那么托管代码和非托管代码之间的封送开销将是巨大的。因此,您可以尝试将此循环放入非托管代码中,以便仅对该方法执行一次调用。然后,您只需支付一次编组费用,整个繁重的工作将在非托管代码中完成。

就将其转换为托管 C++ 而言,我非常怀疑这会给您带来比开始时更好的东西(即完全托管的 C# 代码)。

You could leave the unmanaged method but you should not call it very often from managed code. For example if you are calling the unmanaged method in a tight loop then the overhead of marshaling between managed and unmanaged code will be enormous. So you could try putting this loop inside the unmanaged code so that you perform only a single call to the method. Then you will pay the marshaling price only once and the whole heavylifting will be done in unmanaged code.

As far as converting it to managed C++ is concerned, I highly doubt this would bring you anything better than what you started with (i.e. fully managed C# code).

橘虞初梦 2024-12-29 19:38:08

您可以尝试一下纯 C# 实现是否更快:

static int lzc(int x)
{
    x |= (x >> 1);
    x |= (x >> 2);
    x |= (x >> 4);
    x |= (x >> 8);
    x |= (x >> 16);

    x = x - ((x >> 1) & 0x55555555);
    x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
    x = (x + (x >> 4)) & 0x0f0f0f0f;
    x = x + (x >> 8);
    x = x + (x >> 16);
    return 32 - (x & 0x0000003f);
}

You can try if a pure C# implementation is faster:

static int lzc(int x)
{
    x |= (x >> 1);
    x |= (x >> 2);
    x |= (x >> 4);
    x |= (x >> 8);
    x |= (x >> 16);

    x = x - ((x >> 1) & 0x55555555);
    x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
    x = (x + (x >> 4)) & 0x0f0f0f0f;
    x = x + (x >> 8);
    x = x + (x >> 16);
    return 32 - (x & 0x0000003f);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文