简体   繁体   English

从C#优化C ++调用

[英]Optimizing C++ call from C#

I am calling a two C++ function calls from C# my code is below. 我正在从C#调用两个C ++函数调用,我的代码如下。

[DllImport("A.dll", CallingConvention = CallingConvention.StdCall, EntryPoint = "FuncA"), System.Security.SuppressUnmanagedCodeSecurity]
    public static extern void FuncA(UInt64 functionID);

In C++ code is : 在C ++中的代码是:

EXTERN_C void STDMETHODCALLTYPE FuncA(UINT_PTR functionId)
{
    return;
}

This function is being called from C# to C++ for about 2 million times. 从C#到C ++调用此函数的次数约为200万次。 Without this function call my web request is getting completed in 5.9 seconds.. And with this function call i am getting 7.1 seconds.. 没有此功能调用,我的Web请求将在5.9秒内完成。而通过此功能调用,我将获得7.1秒。

Approx 15% overhead. 大约15%的开销。 Already i have used "SuppressUnmanagedCodeSecurity" by seeing a post, which reduced the overhead from 30% to 15 % .. But is there any other way to reducing this 15% overhead.?? 我已经通过查看帖子使用了“ SuppressUnmanagedCodeSecurity”,将开销从30%减少到了15%..但是还有其他方法可以减少15%的开销。

Update1: UPDATE1:

The function ID needs to be sent to C++ for each and every function calls of C#. 对于每个C#函数调用,都需要将函数ID发送到C ++。 The C++ function is not an empty function. C ++函数不是空函数。 It needs to store the function IDs in a STL and another thread will process it. 它需要将功能ID存储在STL中,然后另一个线程将对其进行处理。 I am doing a .NET profiler sort of thing. 我正在做.NET探查器之类的事情。 I need to profile each and every function calls . 我需要分析每个函数调用。 This FuncA C++ function will get called from injected helper functions. 此FuncA C ++函数将从注入的帮助器函数中调用。

Thanks, 谢谢,

./Rahul ./Rahul

As a longshot you can create a managed C++ CLI static library which calls the unmanaged C++ functions with lower performance decrease and add as reference to the C++/CLI library in your C# project. 从长远来看,您可以创建一个托管C ++ CLI静态库,该静态库调用性能较低的非托管C ++函数,并在C#项目中添加对C ++ / CLI库的引用。 The C# application can then make managed method calls to the C++ CLI library linked, which in turn can make unmanaged method calls. 然后,C#应用程序可以对链接的C ++ CLI库进行托管方法调用,而C ++ CLI库又可以进行非托管方法调用。 Although this results in some indirection, it may provide some performance increase. 尽管这会导致某种间接影响,但可能会提高性能。

What does the C++ function do? C ++函数做什么? Is it really an empty method? 真的是空方法吗? Is it a necessary step in your chain of actions to perform? 这是您执行动作链中的必要步骤吗?

Perhaps it is faster to rewrite this method to C#, to incorporate it in the workflow there, without having to invoke external (probably unsafe) methods. 也许将这种方法重写为C#以便将其合并到那里的工作流程中更快,而不必调用外部(可能是不安全的)方法。

If the action being performed by the C++ code has nothing to do with the rest of your flow, perhaps you might win some time by having it run in a background worker, or a separate thread, so your C# code doesn't have to wait for the code to run. 如果C ++代码执行的操作与其余流程无关,则也许可以通过让它在后台工作程序或单独的线程中运行来赢得一些时间,因此您的C#代码不必等待代码运行。 I suppose this might be a valid option, since your method returns void. 我想这可能是一个有效的选项,因为您的方法返回void。

Perhaps a little more information about why and how might help us to find a better suited answer. 也许更多有关为什么以及如何帮助我们找到更合适答案的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM