简体   繁体   English

将C / C ++代码称为MEX文件与纯C / C ++

[英]Calling C/C++ Code as MEX File vs. Pure C/C++

I personally love high level programming languages. 我个人喜欢高级编程语言。 For proof-of-concept stuff, MATLAB is great. 对于概念验证的东西,MATLAB很棒。 Plus you can easily visualize almost anything with MATLAB. 此外,您可以使用MATLAB轻松查看几乎任何内容。

However, I often need to write C or C++ code for the sake of speed. 但是,为了提高速度,我经常需要编写C或C ++代码。 Visualizations in C/C++ are a pain in the neck though. 然而,C / C ++中的可视化是一个痛苦的问题。 In an ideal world I want MATLAB visualization tools at C/C++ speeds. 在理想的世界中,我想要C / C ++速度的MATLAB可视化工具。 For me that implies I should MEX the necessary C/C++ functions and just call them from a MATLAB script, using MATLAB's tools to perform the visualizations. 对我而言,这意味着我应该使用必要的C / C ++函数,并从MATLAB脚本中调用它们,使用MATLAB的工具来执行可视化。 Ideally this gives me the best of both worlds. 理想情况下,这给了我两全其美。 However, I don't want to end up with slow C/C++ run times as a result of calling the function via MEX files. 但是,由于通过MEX文件调用该函数,我不想最终得到缓慢的C / C ++运行时间。

Do I sacrifice a the 10x-100x speed gains of C++ when calling C/C++ functions as compiled MEX functions? 在将C / C ++函数作为编译的MEX函数调用时,我是否牺牲了C ++10x-100x速度增益 That is, does mexFunction(param1, param2) as called from a MATLAB script necessarily run slower than running the compiled binary? 也就是说,从MATLAB脚本调用的mexFunction(param1, param2)必须比运行已编译的二进制文件运行得慢?

I think to answer this question you must think about what really causes the overhead. 我想回答这个问题你必须考虑究竟是什么导致了开销。 Each function call to mex itself causes an overhead and further passing the data to mex (to my experience only that direction, not passing back the result) also causes some overhead. 每个函数调用mex本身都会导致开销,并进一步将数据传递给mex(根据我的经验,只有那个方向,而不是传回结果)也会导致一些开销。 I assume the primary reason is that M-Code is copy-on-write optimized which means my code never copied the input data, but the mex implementation does receive a copy. 我假设主要原因是M-Code是写时复制优化的,这意味着我的代码从不复制输入数据,但是mex实现确实收到了副本。 To give an example where mex behaved "bad", I think we all agree that C++ is faster iterating and that mathworks probably has qualified programmers, so why was I able to beat the performance implementing binary search in MATLAB? 举一个mex表现“糟糕”的例子,我想我们都同意C ++的迭代速度更快,而且mathworks可能有合格的程序员,那么为什么我能在MATLAB中击败实现二进制搜索的性能呢? . In this case passing the data to the mex function simply made it slow. 在这种情况下,将数据传递给mex函数只会使其变慢。 A lot of data was passed for which you had to pay the overhead and finally the data was barely touched (binary search). 传递了大量数据,您必须为此支付开销,最后几乎没有触及数据(二进制搜索)。

Finally, how large is the overhead really? 最后,真正的开销有多大? For a nop call, it is only 0.00001s , (No input, no output, no calculation). 对于nop调用,它只有0.00001s ,(无输入,无输出,无计算)。 For passing data, I don't have any detailed benchmark, but from the binary search example I linked above it must be somewhere below 0.5s/GB. 对于传递数据,我没有任何详细的基准测试,但是从上面链接的二进制搜索示例中,它必须低于0.5s / GB。

Now do the math for your case and decide if it is worth switching to c++. 现在为你的案例做数学计算,并决定是否值得切换到c ++。

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

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