I am aware that C/C++ is a lower-level language and generates relatively optimized machine code when we compare with any other high-level language. But I guess there is pretty much more than that, which is also evident from the practice.
我知道C/ c++是一种低级语言,当我们与其他高级语言进行比较时,它会生成相对优化的机器代码。但我想还有更多,这也从实践中可以看出。
When I do simple calculations like montecarlo averaging of a Gaussian sample collection or so, I see there is not much of a difference between a C++ implementation or MATLAB implementation, sometimes in fact MATLAB performs a bit better in time.
当我做一些简单的计算,比如对高斯样本集合的montecarlo平均值,我发现在c++实现和MATLAB实现之间并没有太大的区别,实际上MATLAB有时会在时间上表现得更好。
When I move on to larger scale simulations with thousands of lines of code, slowly the real picture shows up. C++ simulations show superior performance like 100x better in time complexity than an equivalent MATLAB implementation.
当我使用数千行代码进行更大范围的模拟时,慢慢地,真实的画面就出现了。c++仿真显示了比同等的MATLAB实现更好的时间复杂度100倍的性能。
The code in C++ most of the times, is pretty much serial and no hi-fi optimization is done explicitly. Whereas, as per my awareness, MATLAB inherently does a lot of optimization. This shows up for example when I try to generate a huge chunk of random samples, where as the equivalent in C++ using some library like IT++/GSL/Boost performs relatively slower (the algorithm used is the same namely mt19937).
大多数时候c++中的代码都是串行的,并且没有显式地进行高保真优化。然而,就我所知,MATLAB天生做了很多优化。例如,当我尝试生成大量的随机样本时,比如在c++中使用一些类似于c++ /GSL/Boost的库,它们的执行速度相对较慢(使用的算法也就是mt19937)。
My question is simply to know if there is a simpler tradeoff between MATLAB/C++ in performance. Is it just like what people say, "Whenever you can, C/C++ is the better"(The frequently experienced)?. In a different perspective, "What is MATLAB good for, other than comfort?"
我的问题只是想知道MATLAB/ c++在性能上是否存在更简单的权衡。是不是就像人们常说的那样,“只要你能,C/ c++就更好”(最常经历的)?从另一个角度来看,“MATLAB除了舒适之外还有什么用?”
By the way, I don't see coding efficiency parameter being significant here, thinking of the same programmer in both cases. And also, I think the other alternatives like python,R are not relevant here. But dependence on the specific libraries we use should be interesting.
顺便说一下,我认为在这两种情况下,考虑到同一个程序员,编码效率参数并不重要。而且,我认为其他的选择,比如python R,在这里不相关。但是对我们使用的特定库的依赖应该是有趣的。
[I am a phd student in Coding Theory in communication systems. I do simulations using matlab/C++ all the time, and have reasonable experience of coding few 10K's of lines in both cases]
我是通信系统编码理论的博士生。我一直在用matlab/ c++进行仿真,在两种情况下都有编写10K行代码的合理经验]
9 个解决方案
#1
79
I have been using Matlab and C++ for about 10 years. For every numerical algorithms implemented for my research, I always start from prototyping with Matlab and then translate the project to C++ to gain a 10x to 100x (I am not kidding) performance improvement. Of course, I am comparing optimized C++ code to the fully vectorized Matlab code. On average, the improvement is about 50x.
我使用Matlab和c++已经有10年了。对于我所研究的每一种数值算法,我总是从用Matlab进行原型设计开始,然后将项目转换为c++,以获得10倍到100倍(我不是在开玩笑)的性能提升。当然,我正在比较优化的c++代码和完全向量化的Matlab代码。平均来说,进步是50倍。
There are lot of subtleties behind both of the two programming languages, and the following are some misunderstandings:
这两种编程语言背后都有很多微妙之处,以下是一些误解:
-
Matlab is a script language but C++ is compiled
Matlab是一种脚本语言,但是c++是编译的
Matlab uses JIT compiler to translate your script to machine code, you can improve your speed at most by a factor 1.5 to 2 by using the compiler that Matlab provides.
Matlab使用JIT编译器将脚本翻译成机器码,使用Matlab提供的编译器最多可以提高1.5到2倍的速度。
-
Matlab code might be able to get fully vectorized but you have to optimize your code by hand in C++
Matlab代码可能能够完全向量化,但是您必须手工使用c++优化代码
Fully vectorized Matlab code can call libraries written in C++/C/Assembly (for example Intel MKL). But plain C++ code can be reasonably vectorized by modern compilers.
完全矢量化的Matlab代码可以调用用c++ /C/Assembly编写的库(例如Intel MKL)。但是普通的c++代码可以被现代编译器合理地量化。
-
Toolboxes and routines that Matlab provides should be very well tuned and should have reasonable performance
Matlab提供的工具箱和例程应该进行很好的调优,并且应该具有合理的性能
No. Other than linear algebra routines, the performance is generally bad.
不。除了线性代数例程之外,性能通常很差。
The reasons why you can gain 10x~100x performance in C++ comparing to vectorized Matlab code:
与矢量化的Matlab代码相比,您可以在c++中获得10x~100x性能的原因是:
-
Calling external libraries (MKL) in Matlab costs time.
在Matlab中调用外部库(MKL)需要花费时间。
-
Memory in Matlab is dynamically allocated and freed. For example, small matrices multiplication:
A = B*C + D*E + F*G
requires Matlab to create 2 temporary matrices. And in C++, if you allocate your memory before hand, you create NONE. And now imagine you loop that statement for 1000 times. Another solution in C++ is provided by C++11 Rvalue reference. This is the one of the biggest improvement in C++, now C++ code can be as fast as plain C code.Matlab中的内存是动态分配和释放的。例如,小矩阵乘法:A = B*C + D*E + F*G需要Matlab创建两个临时矩阵。在c++中,如果事先分配内存,就不会创建任何内存。现在假设你将这个语句循环1000次。c++中的另一个解决方案是由c++ 11 Rvalue引用提供的。这是c++中最大的改进之一,现在c++代码可以和普通的C代码一样快。
-
If you want to do parallel processing, Matlab model is multi-process and the C++ way is multi-thread. If you have many small tasks needing to be parallelized, C++ provides linear gain up to many threads but you might have negative performance gain in Matlab.
如果要进行并行处理,Matlab模型是多进程的,c++方法是多线程的。如果您有许多小任务需要并行化,那么c++可以提供最多多个线程的线性增益,但是在Matlab中可能会有负性能增益。
-
You pass large objects, chunk of memory by reference in C++ but it's quite tricky in Matlab.
在c++中通过引用传递大对象,内存块,但是在Matlab中很复杂。
-
Vectorization in C++ involves using intrinsics/assembly, and sometimes SIMD vectorization is only possible in C++.
在c++中,向量化涉及到使用intrinsic /assembly,而且有时候SIMD向量化只能在c++中实现。
-
Pointers do wonders in C++. Imaging swapping two matrices A and B: In Matlab, it involves a 3rd matrix and requires 3*N*N elementary copy operations. But in C++, this is done by pointers with close to zero cost.
在c++中,指针可以创造奇迹。图像交换两个矩阵A和B:在Matlab中,它涉及一个第三矩阵,需要3*N*N的基本复制操作。但是在c++中,这是通过指针实现的,成本接近于零。
-
In C++, it is possible for an experienced programmer to completely avoid L2 cache miss and even L1 cache miss, hence pushing CPU to its theoretical throughput limit. Performance of Matlab can lag behind C++ by a factor of 10x due to this reason alone.
在c++中,有经验的程序员可以完全避免L2缓存丢失甚至L1缓存丢失,从而将CPU推到理论上的吞吐量限制。仅因为这个原因,Matlab的性能就落后c++ 10倍。
-
In C++, computational intensive instructions sometimes can be grouped according to their latencies (code carefully in assembly or intrinsics) and dependencies (most of time is done automatically by compiler or CPU hardware), such that theoretical IPC (instructions per clock cycle) could be reached and CPU pipelines are filled.
在c++中,计算密集型指令有时可以根据它们的延迟(在汇编或内部特性中小心地编写代码)和依赖关系(大部分时间由编译器或CPU硬件自动完成)进行分组,这样就可以达到理论的IPC(每个时钟周期的指令)并填充CPU管道。
However, development time in C++ is also a factor of 10x comparing to Matlab!
然而,c++的开发时间也是10x的一个因素,与Matlab相比!
The reasons why you should use Matlab instead of C++:
使用Matlab代替c++的原因:
-
Data visualization. I think my career can go on without C++ but I won't be able to survive without Matlab just because it can generate beautiful plots!
数据可视化。我认为没有c++我的事业可以继续下去,但是如果没有Matlab,我将无法生存,因为它可以生成漂亮的情节!
-
Low efficiency but mathematically robust build-in routines and toolboxes. Get the correct answer first and then talk about efficiency. People can make subtle mistakes in C++ (for example implicitly convert double to int) and get sort of correct results.
效率低但在数学上健壮的内置例程和工具箱。首先得到正确的答案,然后讨论效率。人们可以在c++中犯一些微妙的错误(例如隐式地将double转换为int),并得到一些正确的结果。
-
Express your ideas and present your code to your colleagues. Matlab code is much easier to read and much shorter than C++, and Matlab code can be correctly executed without compiler. I just refuse to read other people's C++ code. I don't even use C++ GNU scientific libraries because the code quality is not guaranteed. It is dangerous for a researcher/engineer to use a C++ library as a black box and take the accuracy as granted. Even for commercial C/C++ libraries, I remember Intel compiler had a sign error in its sin() function last year and numerical accuracy problems also occurred in MKL.
表达你的想法,并向你的同事展示你的代码。Matlab代码更易于读取,比c++短得多,而且Matlab代码可以在没有编译器的情况下正确执行。我只是拒绝阅读别人的c++代码。我甚至不使用c++ GNU科学库,因为代码质量没有保证。对于研究者/工程师来说,使用c++库作为一个黑盒子,并将其准确性视为理所当然,这是很危险的。即使对于商业的C/ c++库,我也记得去年Intel compiler在sin()函数中有一个符号错误,MKL中也出现了数值精度问题。
Last but not the least:
最后但并非最不重要:
Because once Matlab code is vectorized, there is not much left for a programmer to optimize, Matlab code performance is much less sensitive to the quality of the code comparing with C++ code. Therefore it is best to optimize computation algorithms in Matlab, and marginally better algorithms normally have marginally better performance in Matlab. On the other hand, algorithm test in C++ requires decent programmer to write algorithms optimized more or less in the same way, and to make sure the compiler does not optimize the algorithms differently.
因为一旦Matlab代码被量化了,程序员就没有多少可以优化的了,而Matlab代码的性能与c++代码相比,对代码质量的敏感度要低得多。因此,最好在Matlab中对计算算法进行优化,一般情况下,较优的算法在Matlab中会有较好的性能。另一方面,c++中的算法测试需要优秀的程序员以相同的方式编写或多或少优化过的算法,并确保编译器不会以不同的方式优化算法。
#2
8
In my experience (several years of Computer Vision and image processing in both languages) there is no simple answer to this question, as Matlab performance depends strongly (and much more than C++ performance) on your coding style.
根据我的经验(多年的计算机视觉和图像处理两种语言),这个问题没有简单的答案,因为Matlab的性能很大程度上依赖于您的编码风格(而且比c++的性能要高得多)。
Generally, Matlab wraps the classic C++ / Fortran based linear algebra libraries. So anything like x = A\b
is going to be very fast. Also, Matlab does a good job in choosing the most efficient solver for these types of problems, so for x = A\b
Matlab will look at the size of your matrices and chose the appropriate low-level routines.
一般来说,Matlab封装了基于c++ / Fortran的线性代数库。所以x = A\b的速度很快。另外,Matlab在选择最有效的解决这些类型问题的解决方案方面做得很好,所以对于x = a \b Matlab,将会考虑矩阵的大小,并选择适当的低级例程。
Matlab also shines in data manipulation of large matrices if you "vectorize" your code, i.e. if you avoid for
loops and use index arrays or boolean arrays to access your data. This stuff is highly optimised.
Matlab在处理大矩阵的数据时也很有用,如果你“向量化”你的代码,例如如果你避免循环,使用索引数组或布尔数组来访问你的数据。这是高度优化的。
For other routines, some are written in Matlab code, while others point to a C/C++ implementation (e.g. the Delaunay stuff). You can check this yourself by typing edit some_routine.m
. This opens the code and you see whether it is all Matlab or just a wrapper for something compiled.
对于其他例程,一些是用Matlab代码编写的,而另一些则指向C/ c++实现(例如Delaunay之类)。您可以通过输入edit some_routing .m来检查这一点。这将打开代码,您可以看到它是否全部是Matlab,或者仅仅是编译过的东西的包装器。
Matlab, I think, is primarily for comfort - but comfort translates to coding time and ultimately money which is why Matlab is used in the industry. Also, it is easy to learn for engineers from other fields than computer science, with little training in programming.
我认为,Matlab主要是为了舒适,但舒适可以转化为编码时间和最终的金钱,这也是Matlab在工业中使用的原因。此外,对于计算机科学以外的其他领域的工程师来说,在编程方面的培训很少,这也很容易。
#3
5
As a PhD Student too, and a 10years long Matlab user, I'm glad to share my POV:
作为一名博士生,还有一个10年的Matlab用户,我很高兴与大家分享我的POV:
Matlab is a great tool for developing and prototyping algorithms, especially when dealing with GUIs, high level analysis (Frequency Domain, LS Optimization etc.): fast coding, powerful syntaxis (think about [],{},: etc.).
Matlab是开发和原型算法的一个很好的工具,特别是在处理gui、高级分析(频域、LS优化等):快速编码、强大的语法(想想[]、{}等)。
As soon as your processing chain is more stable and defined and data dimentions grows move to C/C++.
只要您的处理链更加稳定和定义,并且数据维度增长,就会转移到C/ c++。
The main Matlab limit rises when considering its language is script-like: as long as you avoid any cicle (using arrayfun, cellfun or other matrix procedures) performances are high since the called subroutine is again in C/C++.
当考虑到它的语言是脚本式的时,主要的Matlab限制会增加:只要您避免使用任何cicle(使用arrayfun、cellfun或其他矩阵过程),性能就会很高,因为调用的子例程在C/ c++中也是如此。
#4
4
Your question is difficult to answer. In general C++ is faster, but if make use of the well written algorithms of Matlab it can outperform C++. In some cases Matlab can parallelize your code which has to be done manually in many cases for C++. Mathlab can kind of export C++ code.
你的问题很难回答。一般来说,c++比较快,但是如果使用Matlab编写良好的算法,它可以比c++更好。在某些情况下,Matlab可以并行化您的代码,这在很多情况下c++都需要手工完成。Mathlab可以导出c++代码。
So my conclusion is, that you have to measure the performance of both programs to get an answer. But then you compare your two implementations and not Matlab and C++ in general.
我的结论是,你必须测量两个程序的性能才能得到答案。然后比较两个实现,而不是Matlab和c++。
#5
4
Matlab does very well with linear algebra and array/matrix operations, since they seem to have been doing some extra optimizations on the underlying operations - if you want to beat Matlab there, you would need a similarly optimized BLAS/LAPACK library.
Matlab在线性代数和数组/矩阵运算方面做得很好,因为它们似乎在底层的操作上做了一些额外的优化——如果你想在这里打败Matlab,你需要一个类似的优化的BLAS/LAPACK库。
As an interpreted language, Matlab loses time whenever a Matlab function is called, due to internal overhead, which traditionally meant that Matlab loops were slow. This has been alleviated somewhat in recent years thanks to significant improvement in the JIT compiler (search for "performance" questions on Matlab on SO for examples). As a consequence of the function call overhead, all Matlab functions that have not been implemented in C/C++ behind the scenes (call edit functionName
to see whether it's written in Matlab) risks being slower than a C/C++ counterpart.
作为一种解释语言,Matlab在调用Matlab函数时就会失去时间,这是由于内部开销,这通常意味着Matlab的循环很慢。由于JIT编译器的显著改进(例如在Matlab上搜索“性能”问题),近年来这一点得到了一定程度的缓解。作为函数调用开销的结果,所有未在C/ c++中实现的Matlab函数(调用edit functionName来查看它是否在Matlab中编写)都可能比C/ c++函数要慢。
Finally, Matlab attempts to be user friendly, and may do "unnecessary" input checking that can take time (due to function call overhead). For example, if you know that ismember
gets sorted inputs, you can call ismembc
directly (the behind-the-scene compiled function), saving quite a bit of time.
最后,Matlab尝试对用户友好,并可能进行“不必要的”输入检查,这可能需要时间(由于函数调用开销)。例如,如果您知道ismember获得了排序输入,您可以直接调用ismembc(后台编译函数),从而节省了不少时间。
#6
3
I think you can consider the difference in four folds at least.
我想你至少可以考虑四倍的差异。
- Compiled vs Interpreted
- 编译和解释
- Strongly-typed vs Dynamically-typed
- 强类型vs的动态
- Performance vs Fast-prototyping
- 性能和快速原型
- Special strength
- 特殊的力量
For 1-3 can be easily generalized into comparison between two family of programming languages.
对于1-3,可以很容易地推广到两个编程语言族之间的比较。
For 4, MATLAB
is optimized for matrix operations. So if you can vectorize more code in MATLAB
, the performance can be drastically boosted. Conversely, if many loops
are required, never hesitate to use C++
or create a mex
file.
对于4,MATLAB对矩阵运算进行了优化。所以如果你能在MATLAB中对更多的代码进行矢量化,性能就会大大提高。相反,如果需要多个循环,则不要犹豫使用c++或创建一个mex文件。
It is a difficult quesion after all.
这毕竟是个难题。
#7
2
Besides the speed of the final program, you should also take into account the total development time of your code, ie., not only the time to write, but also to debug, etc. Matlab (and its open-source counterpart, Octave) can be good for quick prototyping due to its visualisation capabilities.
除了最终程序的速度之外,您还应该考虑到代码的开发时间。Matlab(及其开放源码的对应程序,Octave)由于其可视化功能,可以用于快速原型设计。
If you're using straight C++ (ie. no matrix libraries), it may take you much longer to write C++ code that's equivalent to Matlab code (eg. there might be no point in spending 10 hours writing C++ code that only runs 10 seconds quicker, compared to a Matlab program that took 5 minutes to write).
如果你使用的是纯c++(即。没有矩阵库),编写等同于Matlab代码的c++代码可能需要更长时间。与用5分钟编写的Matlab程序相比,花10个小时编写c++代码只会快10秒是没有意义的)。
However, there are dedicated C++ matrix libraries, such as Armadillo, which provide a Matlab-like API. This can be useful for writing performance critical code that can be called from Matlab, or for converting Matlab code into "real" programs.
但是,有专门的c++矩阵库,比如Armadillo,它提供了一个类似matlabla的API。这对于编写可以从Matlab调用的性能关键代码或将Matlab代码转换为“真实”程序非常有用。
#8
2
I saw a 5.5x speed improvement when switching from MATLAB to C++. This was for a robot controller- lots of loops and ode solving. I spent many hours trying to optimize the MATLAB code, hardly any time optimizing the C++ (I'm sure it could have been 10x faster with a little more effort).
从MATLAB切换到c++时,速度提高了5.5倍。这是为了一个机器人控制器-许多循环和ode解决。我花了很多时间试图优化MATLAB代码,几乎没有时间对c++进行优化(我敢肯定,如果再多做一点努力,它的速度会快10倍)。
However, it was easy to add a GUI for the MATLAB code, so I still use it more often. Like others have said, it was nice to prototype first on MATLAB. That made the implementation on C++ much simpler.
但是,为MATLAB代码添加GUI很容易,所以我仍然更经常地使用它。就像其他人说的,首先在MATLAB上进行原型化是很好的。这使得c++的实现更加简单。
#9
2
Some Matlab code uses standard linear algebra fictions with multithreading built into it. So, it appears that they are faster than a sequential C code.
一些Matlab代码使用标准的线性代数虚构,并内置多线程。因此,它们看起来比顺序C代码更快。
#1
79
I have been using Matlab and C++ for about 10 years. For every numerical algorithms implemented for my research, I always start from prototyping with Matlab and then translate the project to C++ to gain a 10x to 100x (I am not kidding) performance improvement. Of course, I am comparing optimized C++ code to the fully vectorized Matlab code. On average, the improvement is about 50x.
我使用Matlab和c++已经有10年了。对于我所研究的每一种数值算法,我总是从用Matlab进行原型设计开始,然后将项目转换为c++,以获得10倍到100倍(我不是在开玩笑)的性能提升。当然,我正在比较优化的c++代码和完全向量化的Matlab代码。平均来说,进步是50倍。
There are lot of subtleties behind both of the two programming languages, and the following are some misunderstandings:
这两种编程语言背后都有很多微妙之处,以下是一些误解:
-
Matlab is a script language but C++ is compiled
Matlab是一种脚本语言,但是c++是编译的
Matlab uses JIT compiler to translate your script to machine code, you can improve your speed at most by a factor 1.5 to 2 by using the compiler that Matlab provides.
Matlab使用JIT编译器将脚本翻译成机器码,使用Matlab提供的编译器最多可以提高1.5到2倍的速度。
-
Matlab code might be able to get fully vectorized but you have to optimize your code by hand in C++
Matlab代码可能能够完全向量化,但是您必须手工使用c++优化代码
Fully vectorized Matlab code can call libraries written in C++/C/Assembly (for example Intel MKL). But plain C++ code can be reasonably vectorized by modern compilers.
完全矢量化的Matlab代码可以调用用c++ /C/Assembly编写的库(例如Intel MKL)。但是普通的c++代码可以被现代编译器合理地量化。
-
Toolboxes and routines that Matlab provides should be very well tuned and should have reasonable performance
Matlab提供的工具箱和例程应该进行很好的调优,并且应该具有合理的性能
No. Other than linear algebra routines, the performance is generally bad.
不。除了线性代数例程之外,性能通常很差。
The reasons why you can gain 10x~100x performance in C++ comparing to vectorized Matlab code:
与矢量化的Matlab代码相比,您可以在c++中获得10x~100x性能的原因是:
-
Calling external libraries (MKL) in Matlab costs time.
在Matlab中调用外部库(MKL)需要花费时间。
-
Memory in Matlab is dynamically allocated and freed. For example, small matrices multiplication:
A = B*C + D*E + F*G
requires Matlab to create 2 temporary matrices. And in C++, if you allocate your memory before hand, you create NONE. And now imagine you loop that statement for 1000 times. Another solution in C++ is provided by C++11 Rvalue reference. This is the one of the biggest improvement in C++, now C++ code can be as fast as plain C code.Matlab中的内存是动态分配和释放的。例如,小矩阵乘法:A = B*C + D*E + F*G需要Matlab创建两个临时矩阵。在c++中,如果事先分配内存,就不会创建任何内存。现在假设你将这个语句循环1000次。c++中的另一个解决方案是由c++ 11 Rvalue引用提供的。这是c++中最大的改进之一,现在c++代码可以和普通的C代码一样快。
-
If you want to do parallel processing, Matlab model is multi-process and the C++ way is multi-thread. If you have many small tasks needing to be parallelized, C++ provides linear gain up to many threads but you might have negative performance gain in Matlab.
如果要进行并行处理,Matlab模型是多进程的,c++方法是多线程的。如果您有许多小任务需要并行化,那么c++可以提供最多多个线程的线性增益,但是在Matlab中可能会有负性能增益。
-
You pass large objects, chunk of memory by reference in C++ but it's quite tricky in Matlab.
在c++中通过引用传递大对象,内存块,但是在Matlab中很复杂。
-
Vectorization in C++ involves using intrinsics/assembly, and sometimes SIMD vectorization is only possible in C++.
在c++中,向量化涉及到使用intrinsic /assembly,而且有时候SIMD向量化只能在c++中实现。
-
Pointers do wonders in C++. Imaging swapping two matrices A and B: In Matlab, it involves a 3rd matrix and requires 3*N*N elementary copy operations. But in C++, this is done by pointers with close to zero cost.
在c++中,指针可以创造奇迹。图像交换两个矩阵A和B:在Matlab中,它涉及一个第三矩阵,需要3*N*N的基本复制操作。但是在c++中,这是通过指针实现的,成本接近于零。
-
In C++, it is possible for an experienced programmer to completely avoid L2 cache miss and even L1 cache miss, hence pushing CPU to its theoretical throughput limit. Performance of Matlab can lag behind C++ by a factor of 10x due to this reason alone.
在c++中,有经验的程序员可以完全避免L2缓存丢失甚至L1缓存丢失,从而将CPU推到理论上的吞吐量限制。仅因为这个原因,Matlab的性能就落后c++ 10倍。
-
In C++, computational intensive instructions sometimes can be grouped according to their latencies (code carefully in assembly or intrinsics) and dependencies (most of time is done automatically by compiler or CPU hardware), such that theoretical IPC (instructions per clock cycle) could be reached and CPU pipelines are filled.
在c++中,计算密集型指令有时可以根据它们的延迟(在汇编或内部特性中小心地编写代码)和依赖关系(大部分时间由编译器或CPU硬件自动完成)进行分组,这样就可以达到理论的IPC(每个时钟周期的指令)并填充CPU管道。
However, development time in C++ is also a factor of 10x comparing to Matlab!
然而,c++的开发时间也是10x的一个因素,与Matlab相比!
The reasons why you should use Matlab instead of C++:
使用Matlab代替c++的原因:
-
Data visualization. I think my career can go on without C++ but I won't be able to survive without Matlab just because it can generate beautiful plots!
数据可视化。我认为没有c++我的事业可以继续下去,但是如果没有Matlab,我将无法生存,因为它可以生成漂亮的情节!
-
Low efficiency but mathematically robust build-in routines and toolboxes. Get the correct answer first and then talk about efficiency. People can make subtle mistakes in C++ (for example implicitly convert double to int) and get sort of correct results.
效率低但在数学上健壮的内置例程和工具箱。首先得到正确的答案,然后讨论效率。人们可以在c++中犯一些微妙的错误(例如隐式地将double转换为int),并得到一些正确的结果。
-
Express your ideas and present your code to your colleagues. Matlab code is much easier to read and much shorter than C++, and Matlab code can be correctly executed without compiler. I just refuse to read other people's C++ code. I don't even use C++ GNU scientific libraries because the code quality is not guaranteed. It is dangerous for a researcher/engineer to use a C++ library as a black box and take the accuracy as granted. Even for commercial C/C++ libraries, I remember Intel compiler had a sign error in its sin() function last year and numerical accuracy problems also occurred in MKL.
表达你的想法,并向你的同事展示你的代码。Matlab代码更易于读取,比c++短得多,而且Matlab代码可以在没有编译器的情况下正确执行。我只是拒绝阅读别人的c++代码。我甚至不使用c++ GNU科学库,因为代码质量没有保证。对于研究者/工程师来说,使用c++库作为一个黑盒子,并将其准确性视为理所当然,这是很危险的。即使对于商业的C/ c++库,我也记得去年Intel compiler在sin()函数中有一个符号错误,MKL中也出现了数值精度问题。
Last but not the least:
最后但并非最不重要:
Because once Matlab code is vectorized, there is not much left for a programmer to optimize, Matlab code performance is much less sensitive to the quality of the code comparing with C++ code. Therefore it is best to optimize computation algorithms in Matlab, and marginally better algorithms normally have marginally better performance in Matlab. On the other hand, algorithm test in C++ requires decent programmer to write algorithms optimized more or less in the same way, and to make sure the compiler does not optimize the algorithms differently.
因为一旦Matlab代码被量化了,程序员就没有多少可以优化的了,而Matlab代码的性能与c++代码相比,对代码质量的敏感度要低得多。因此,最好在Matlab中对计算算法进行优化,一般情况下,较优的算法在Matlab中会有较好的性能。另一方面,c++中的算法测试需要优秀的程序员以相同的方式编写或多或少优化过的算法,并确保编译器不会以不同的方式优化算法。
#2
8
In my experience (several years of Computer Vision and image processing in both languages) there is no simple answer to this question, as Matlab performance depends strongly (and much more than C++ performance) on your coding style.
根据我的经验(多年的计算机视觉和图像处理两种语言),这个问题没有简单的答案,因为Matlab的性能很大程度上依赖于您的编码风格(而且比c++的性能要高得多)。
Generally, Matlab wraps the classic C++ / Fortran based linear algebra libraries. So anything like x = A\b
is going to be very fast. Also, Matlab does a good job in choosing the most efficient solver for these types of problems, so for x = A\b
Matlab will look at the size of your matrices and chose the appropriate low-level routines.
一般来说,Matlab封装了基于c++ / Fortran的线性代数库。所以x = A\b的速度很快。另外,Matlab在选择最有效的解决这些类型问题的解决方案方面做得很好,所以对于x = a \b Matlab,将会考虑矩阵的大小,并选择适当的低级例程。
Matlab also shines in data manipulation of large matrices if you "vectorize" your code, i.e. if you avoid for
loops and use index arrays or boolean arrays to access your data. This stuff is highly optimised.
Matlab在处理大矩阵的数据时也很有用,如果你“向量化”你的代码,例如如果你避免循环,使用索引数组或布尔数组来访问你的数据。这是高度优化的。
For other routines, some are written in Matlab code, while others point to a C/C++ implementation (e.g. the Delaunay stuff). You can check this yourself by typing edit some_routine.m
. This opens the code and you see whether it is all Matlab or just a wrapper for something compiled.
对于其他例程,一些是用Matlab代码编写的,而另一些则指向C/ c++实现(例如Delaunay之类)。您可以通过输入edit some_routing .m来检查这一点。这将打开代码,您可以看到它是否全部是Matlab,或者仅仅是编译过的东西的包装器。
Matlab, I think, is primarily for comfort - but comfort translates to coding time and ultimately money which is why Matlab is used in the industry. Also, it is easy to learn for engineers from other fields than computer science, with little training in programming.
我认为,Matlab主要是为了舒适,但舒适可以转化为编码时间和最终的金钱,这也是Matlab在工业中使用的原因。此外,对于计算机科学以外的其他领域的工程师来说,在编程方面的培训很少,这也很容易。
#3
5
As a PhD Student too, and a 10years long Matlab user, I'm glad to share my POV:
作为一名博士生,还有一个10年的Matlab用户,我很高兴与大家分享我的POV:
Matlab is a great tool for developing and prototyping algorithms, especially when dealing with GUIs, high level analysis (Frequency Domain, LS Optimization etc.): fast coding, powerful syntaxis (think about [],{},: etc.).
Matlab是开发和原型算法的一个很好的工具,特别是在处理gui、高级分析(频域、LS优化等):快速编码、强大的语法(想想[]、{}等)。
As soon as your processing chain is more stable and defined and data dimentions grows move to C/C++.
只要您的处理链更加稳定和定义,并且数据维度增长,就会转移到C/ c++。
The main Matlab limit rises when considering its language is script-like: as long as you avoid any cicle (using arrayfun, cellfun or other matrix procedures) performances are high since the called subroutine is again in C/C++.
当考虑到它的语言是脚本式的时,主要的Matlab限制会增加:只要您避免使用任何cicle(使用arrayfun、cellfun或其他矩阵过程),性能就会很高,因为调用的子例程在C/ c++中也是如此。
#4
4
Your question is difficult to answer. In general C++ is faster, but if make use of the well written algorithms of Matlab it can outperform C++. In some cases Matlab can parallelize your code which has to be done manually in many cases for C++. Mathlab can kind of export C++ code.
你的问题很难回答。一般来说,c++比较快,但是如果使用Matlab编写良好的算法,它可以比c++更好。在某些情况下,Matlab可以并行化您的代码,这在很多情况下c++都需要手工完成。Mathlab可以导出c++代码。
So my conclusion is, that you have to measure the performance of both programs to get an answer. But then you compare your two implementations and not Matlab and C++ in general.
我的结论是,你必须测量两个程序的性能才能得到答案。然后比较两个实现,而不是Matlab和c++。
#5
4
Matlab does very well with linear algebra and array/matrix operations, since they seem to have been doing some extra optimizations on the underlying operations - if you want to beat Matlab there, you would need a similarly optimized BLAS/LAPACK library.
Matlab在线性代数和数组/矩阵运算方面做得很好,因为它们似乎在底层的操作上做了一些额外的优化——如果你想在这里打败Matlab,你需要一个类似的优化的BLAS/LAPACK库。
As an interpreted language, Matlab loses time whenever a Matlab function is called, due to internal overhead, which traditionally meant that Matlab loops were slow. This has been alleviated somewhat in recent years thanks to significant improvement in the JIT compiler (search for "performance" questions on Matlab on SO for examples). As a consequence of the function call overhead, all Matlab functions that have not been implemented in C/C++ behind the scenes (call edit functionName
to see whether it's written in Matlab) risks being slower than a C/C++ counterpart.
作为一种解释语言,Matlab在调用Matlab函数时就会失去时间,这是由于内部开销,这通常意味着Matlab的循环很慢。由于JIT编译器的显著改进(例如在Matlab上搜索“性能”问题),近年来这一点得到了一定程度的缓解。作为函数调用开销的结果,所有未在C/ c++中实现的Matlab函数(调用edit functionName来查看它是否在Matlab中编写)都可能比C/ c++函数要慢。
Finally, Matlab attempts to be user friendly, and may do "unnecessary" input checking that can take time (due to function call overhead). For example, if you know that ismember
gets sorted inputs, you can call ismembc
directly (the behind-the-scene compiled function), saving quite a bit of time.
最后,Matlab尝试对用户友好,并可能进行“不必要的”输入检查,这可能需要时间(由于函数调用开销)。例如,如果您知道ismember获得了排序输入,您可以直接调用ismembc(后台编译函数),从而节省了不少时间。
#6
3
I think you can consider the difference in four folds at least.
我想你至少可以考虑四倍的差异。
- Compiled vs Interpreted
- 编译和解释
- Strongly-typed vs Dynamically-typed
- 强类型vs的动态
- Performance vs Fast-prototyping
- 性能和快速原型
- Special strength
- 特殊的力量
For 1-3 can be easily generalized into comparison between two family of programming languages.
对于1-3,可以很容易地推广到两个编程语言族之间的比较。
For 4, MATLAB
is optimized for matrix operations. So if you can vectorize more code in MATLAB
, the performance can be drastically boosted. Conversely, if many loops
are required, never hesitate to use C++
or create a mex
file.
对于4,MATLAB对矩阵运算进行了优化。所以如果你能在MATLAB中对更多的代码进行矢量化,性能就会大大提高。相反,如果需要多个循环,则不要犹豫使用c++或创建一个mex文件。
It is a difficult quesion after all.
这毕竟是个难题。
#7
2
Besides the speed of the final program, you should also take into account the total development time of your code, ie., not only the time to write, but also to debug, etc. Matlab (and its open-source counterpart, Octave) can be good for quick prototyping due to its visualisation capabilities.
除了最终程序的速度之外,您还应该考虑到代码的开发时间。Matlab(及其开放源码的对应程序,Octave)由于其可视化功能,可以用于快速原型设计。
If you're using straight C++ (ie. no matrix libraries), it may take you much longer to write C++ code that's equivalent to Matlab code (eg. there might be no point in spending 10 hours writing C++ code that only runs 10 seconds quicker, compared to a Matlab program that took 5 minutes to write).
如果你使用的是纯c++(即。没有矩阵库),编写等同于Matlab代码的c++代码可能需要更长时间。与用5分钟编写的Matlab程序相比,花10个小时编写c++代码只会快10秒是没有意义的)。
However, there are dedicated C++ matrix libraries, such as Armadillo, which provide a Matlab-like API. This can be useful for writing performance critical code that can be called from Matlab, or for converting Matlab code into "real" programs.
但是,有专门的c++矩阵库,比如Armadillo,它提供了一个类似matlabla的API。这对于编写可以从Matlab调用的性能关键代码或将Matlab代码转换为“真实”程序非常有用。
#8
2
I saw a 5.5x speed improvement when switching from MATLAB to C++. This was for a robot controller- lots of loops and ode solving. I spent many hours trying to optimize the MATLAB code, hardly any time optimizing the C++ (I'm sure it could have been 10x faster with a little more effort).
从MATLAB切换到c++时,速度提高了5.5倍。这是为了一个机器人控制器-许多循环和ode解决。我花了很多时间试图优化MATLAB代码,几乎没有时间对c++进行优化(我敢肯定,如果再多做一点努力,它的速度会快10倍)。
However, it was easy to add a GUI for the MATLAB code, so I still use it more often. Like others have said, it was nice to prototype first on MATLAB. That made the implementation on C++ much simpler.
但是,为MATLAB代码添加GUI很容易,所以我仍然更经常地使用它。就像其他人说的,首先在MATLAB上进行原型化是很好的。这使得c++的实现更加简单。
#9
2
Some Matlab code uses standard linear algebra fictions with multithreading built into it. So, it appears that they are faster than a sequential C code.
一些Matlab代码使用标准的线性代数虚构,并内置多线程。因此,它们看起来比顺序C代码更快。