问题描述
我写了简单的C ++代码并在C ++中测试,然后我已经通过 mex file_name.cpp
修改了MATLAB的相同代码,并在MATLAB中运行相同的代码使用与C ++相同的编译器。下面是代码:
int k;
for(int j = 0; j for(int i = 0; i k ++;
}
k / = 10000000
}
这里是MATLAB代码:
double a; int j; int i;
double * k;
for(j = 0; j for(i = 0; i< 10000000; i ++){
a = a + 1;
}
a = a / 10000000;
}
plhs [0] = mxCreateDoubleMatrix(1,1,mxREAL);
k = mxGetPr(plhs [0]);
* k =(double)a;
我已经为MATLAB编辑了这段代码,即改为适当的类型,添加MEX函数等,结果在MATLAB中约为900ms,而在C ++中为3100ms。
我不明白是两个都运行相同的代码和相同的编译器我在命令行中写入 mex -setup
,并选择Visual Studio编译器作为MEX编译器),然而,MATLAB大约快3.5倍。
MATLAB做得更快,C ++不做什么?有人可以请解释我为什么有这么巨大的差异?我已经尝试了一些其他的代码,都是在MATLAB中3-6倍的速度。
我的电脑是64位Windows 7,Visual Studio 2010是用于C ++,MATLAB是R2012b。 b
这可能是因为我的Visual Studio版本?如果我将它更改为VS2012,会更快吗?
感谢, 性能高度依赖于平台,操作系统,编译器等。无论Matlab在这种情况下做了什么,它设法找到一个优化,VS2010编译器没有。我冒昧地猜测,升级到VS2012不会产生实质性的差异,但我可能是错的。毕竟,这是一个不同的编译器。 我承认这有点令人惊讶,但检查你的编译标志,并尝试使用不同的配置分析。如果你的Matlab安装是32位的,这也可能有所作为。 代码中也可能有些微差异,可能是足够轻微,已注意到。您的代码可能与其他可能具有很大性能变化的库进行链接。 这里的教训是,可能非常难以确定 / em>为什么一个东西比另一个好。 编辑:你提到的代码是编译为调试。这只会进一步增加编译器将输出的变化,因为激活调试选项也可能会关闭其他优化,每个编译器都有不同的想法,什么样的调试信息是重要的,值得坚持在代码中。 我建议关闭所有调试选项以获得更一致的输出。我也建议您确保您使用类似的优化级别进行编译,可能是最大可能的或根本不。 I have written simple C++ code and tested it in C++, then I have adapted the same code for MATLAB by Here is MATLAB code: I have edited this code for MATLAB, i.e changing to suitable types, adding MEX-function etc and the results are approx 900ms in MATLAB as opposed to 3100 ms in C++. What I don't understand is both are running the same code and with the same compiler (in MATLAB I write What is MATLAB doing to be that faster and what is C++ not doing? Could somebody please explain me why there is so huge difference? I have tried some other codes, all are 3-6 times faster in MATLAB. My PC is 64-bit Windows 7, Visual Studio 2010 is used for C++, MATLAB is R2012b. Is it possible this is because of my Visual Studio version? If I change it to VS2012, would it be faster? mex -v output is here. Thanks, Performance is highly dependent on platform, OS, compiler, etc. Whatever Matlab is doing in this case, it somehow has managed to find an optimization that the VS2010 compiler did not. I would venture to guess that upgrading to VS2012 would not make a substantial difference, but I could be wrong. It is, after all, a different compiler. I will admit that this is somewhat surprising, but check your compilation flags and try profiling with different configurations. If your Matlab install is 32-bit, that could make a difference, as well. There could also be slight differences in your code, possibly slight enough that you might not have noticed. Your code might be linking against other libraries that could also have wide variation in performance. The lesson here is that it can be very difficult to pin down exactly why one thing performs better than another. EDIT: You have mentioned that the code is compiled for debugging. This only further increases the variation of what compilers will output, since activating debug options may also turn off other optimizations, and each compiler has a different idea of what sort of debug information is important and worth sticking in your code. I would recommend turning off all debug options to get a more consistent output. I would also recommend making sure you are compiling with similar levels of optimization, probably either the greatest possible or not at all. 这篇关于Matlab和c ++在这段代码中的速度比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
mex file_name.cpp
and run the same code in MATLAB which is using the same compiler as C++. Here is the code: int k;
for(int j = 0; j < 100;j++){
for(int i = 0; i < 10000000; i++){
k++;
}
k/=10000000
}
double a;int j;int i;
double* k;
for(j = 0; j < 100;j++){
for(i = 0; i < 10000000; i++){
a = a+1;
}
a = a / 10000000;
}
plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);
k = mxGetPr(plhs[0]);
*k = (double)a;
mex -setup
in command line and selected Visual Studio compiler as MEX compiler), however, MATLAB is around 3.5 times faster.