简洁地解释一下:
=================================
首先声明,Fortran / C是高级语言,MATLAB是解释型语言。
如果你的MATLAB程序有一个大循环,那么每次循环时MATLAB都要解释一次语句,因此你的100分钟当中,有相当一部分不是在运行程序,而是在解释程序。
Fortran / C只是在编译的时候解释一次,将程序转成机器可识别的代码。因此如果有大循环,就应该用Fortran / C。MATLAB只适用于小程序,也就算是个高级计算器。
=============================
其次,关于Fortran / C。都是高级语言,速度上不会有太大区别。
Fortran可以直接进行矩阵操作,因此可以直接把数学公式转换成语句。Fortran的意思就是FORmula TRANslator,适合于做数值计算。
C的优势是指针,因此适合于存在复杂数据结构的问题。但如果要用C实现矩阵操作,就必须自己写繁琐的循环。
明白了吗?
在数学运算(加减乘除等)上面 matlab 和C几乎差不多
但是在逻辑判断上面matlab 就慢的多了
matlab 处理一大堆的if else的程序,显得比较慢
我曾几坐过一个测试,就是算fft 一个是 matlab 一个是 fftw库(c++), 还有一个是我自己写的C++程序,自认为已经把运算量降到最低,但是最后的结果是 matlab 和fftw 几乎一样快,我的程序要慢5倍左右
真的要100分钟么?!!今天用四种语言做十亿次加法运算,c语言2.5s左右,matlab 1.5秒左右 R语言在35s左右 python在95s左右,python最不适合做计算密集型的,有GIL锁
这种比较毫无意思
请你仔细看看matlab的介绍信息
MATLAB is a high-level technical computing language and interactive environment for algorithm development, data visualization, data analysis, and numeric computation. Using the MATLAB product, you can solve technical computing problems faster than with traditional programming languages, such as C, C++, and Fortran.
You can use MATLAB in a wide range of applications, including signal and image processing, communications, control design, test and measurement, financial modeling and analysis, and computational biology. Add-on toolboxes (collections of special-purpose MATLAB functions, available separately) extend the MATLAB environment to solve particular classes of problems in these application areas.
MATLAB provides a number of features for documenting and sharing your work. You can integrate your MATLAB code with other languages and applications, and distribute your MATLAB algorithms and applications. Features include:
*
High-level language for technical computing
*
Development environment for managing code, files, and data
*
Interactive tools for iterative exploration, design, and problem solving
*
Mathematical functions for linear algebra, statistics, Fourier analysis, filtering, optimization, and numerical integration
*
2-D and 3-D graphics functions for visualizing data
*
Tools for building custom graphical user interfaces
*
Functions for integrating MATLAB based algorithms with external applications and languages, such as C, C++, Fortran, Java™, COM, and Microsoft® Excel
另外我觉得速度不光取决于语言问题
我前一段时间写了一个程序,c#运行135s,matlab运行3.5s
你给的例子里,我认为matlab要比用C++实现慢的多,因为matlab要反复调用同一个函数,简单的加法运算或许其内部还是按照矩阵的方式来处理的,而C++没这方面的问题,函数只需调用一次,加法若干个机器指令就可以完成。但是,如果是运算量很大的数值计算的话,相信matlab更快.