在每个调用c ++的c#中构建一个整数数组,或者在c ++中构建并传递给c#? [英] Build an array of ints in c# each calling c++ or build in c++ and pass to c#?

查看:161
本文介绍了在每个调用c ++的c#中构建一个整数数组,或者在c ++中构建并传递给c#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用C ++编写了一个函数,让我可以通过内部函数利用新的Intel RdRand数字随机数生成器.

I have written a function in C++ to let me take advantage of the new Intel RdRand digital random number generator via an intrinsic function.

__declspec(dllexport) int __stdcall GetRdRand32(PUINT32 pValue)
{
    return _rdrand32_step(pValue);
}

我已经包装好它,以便可以通过PInvoke在C#中使用它,并且可以按以下方式正常工作:

I have wrapped it so that I can use it in C# via PInvoke and it is working fine as follows:

[DllImport("CppDynamicLinkLibrary.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int GetRdRand32(out UInt32 str);

我的用例通常涉及请求多个随机数,尽管一次(每个请求者)可能仅数百个.我的问题是,无论如何我都在使用C ++,将另一个可以返回随机数的动态数组(或向量)的函数放在一起是否有意义,即,与仅多次调用C ++ DLL相比,这会大大提高性能吗? ?性能是一个令人担忧的问题,因为它将在服务器应用程序上运行,该服务器应用程序可能在相似的时间向许多客户端发送约200个随机数

My use case could often involve requesting more than one random number although probably only on the order of hundreds at a time (per requester). My question is, as I'm using C++ anyway, would it make sense to put together another function that can return a dynamic array (or vector) of random numbers, i.e. would this greatly improve performance over just making multiple calls to the C++ DLL? Performance is a concern because this will be on a server application that could be sending ~200 random numbers to many clients at similar times

如果值得这样做,我将如何去做呢?我正在按照以下思路进行思考,尽管我的猜测是找到一种将向量导入C#的方法很容易成为性能问题?

If it is worthwhile doing, how would I go about doing it? I was thinking something along the lines of the following, although my guess is finding a way to get the vector into C# could easily be a performance concern?

__declspec(dllexport) void __stdcall vGetRdRand32(std::vector<UINT32> &pArray)
{
    for (std::vector<UINT32>::iterator It = pArray.begin(); It != pArray.end(); It++ )
        _rdrand32_step(&(*It));
}

最后,元帅将比后一种方法更好,如果可以的话,有人能指出我正确的方向吗?

Finally, would Marshal.Copy be better than the latter approach, could anyone point me in right direction if it would be?

推荐答案

当然,从单个调用中获取200个随机数比从200个不同调用中获取200个随机数要快.它甚至可能快很多倍.但是,您所讲的时间可能相差毫秒.因此,这可能不值得做.几毫秒的差异会对您的应用程序的整体性能产生明显的影响吗?

Certainly, getting 200 random numbers from a single call will be faster than getting 200 random numbers from 200 different calls. It might even be many times faster. But it's likely that you're talking a difference of milliseconds. So it might not be worth doing. Will the difference of a few milliseconds make a noticeable difference to the overall performance of your application?

如果您决定这样做,您可能不想弄混vector,而是想弄混UINT32[].在C#和C ++之间封送vector最多是困难的.出于所有实际目的,这是不可能的.

If you do decide to do it, you probably don't want to mess with vector, but rather with UINT32[]. Marshaling a vector between C# and C++ would be difficult at best. For all practical purposes, impossible.

有关如何编组的示例,请参见封送不同类型的数组数组.

See Marshaling Different Types of Arrays for examples of how to marshal arrays.

您可能需要在C#中分配数组,并将其和大小一起传递给C ++函数.这样,您不必担心释放内存.如果您有C ++代码分配数组并返回它,则C#代码将必须调用C ++函数来释放内存.

You'll probably want to allocate the array in C# and pass it along with the size to the C++ function. That way, you don't have to worry about deallocating the memory. If you have the C++ code allocate the array and return it, then the C# code will have to call a C++ function to deallocate the memory.

这篇关于在每个调用c ++的c#中构建一个整数数组,或者在c ++中构建并传递给c#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆