我不时地读到Fortran在繁重的计算中比C更快。这是真的吗?我必须承认我几乎不懂Fortran,但是到目前为止我看到的Fortran代码并没有显示出该语言具有C语言所不具备的特性。

如果是真的,请告诉我原因。请不要告诉我什么语言或库适合处理数字,我不打算写一个应用程序或库来做这个,我只是好奇。


当前回答

有趣的是,这里的很多答案都来自于不懂语言。这对于那些打开过旧的FORTRAN 77代码并讨论过其弱点的C/ c++程序员来说尤其如此。

我认为速度问题主要是C/ c++和Fortran之间的问题。在大型代码中,它总是取决于程序员。有一些语言特性是Fortran优于的,而一些特性是C优于的。所以,在2011年,没有人能真正说出哪一个更快。

About the language itself, Fortran nowadays supports Full OOP features and it is fully backward compatible. I have used the Fortran 2003 thoroughly and I would say it was just delightful to use it. In some aspects, Fortran 2003 is still behind C++ but let's look at the usage. Fortran is mostly used for Numerical Computation, and nobody uses fancy C++ OOP features because of speed reasons. In high performance computing, C++ has almost no place to go(have a look at the MPI standard and you'll see that C++ has been deprecated!).

现在,您可以简单地使用Fortran和C/ c++进行混合语言编程。Fortran中甚至有GTK+的接口。有免费的编译器(gfortran, g95)和许多优秀的商业编译器。

其他回答

Fortran和C之间的速度差异更多的是编译器优化和特定编译器使用的底层数学库的函数。Fortran没有什么固有的特性可以使它比C更快。

不管怎样,一个优秀的程序员可以用任何语言编写Fortran。

是的,在1980年;在2008年?取决于

当我开始专业编程时,Fortran的速度优势正受到挑战。我记得我在Dr. Dobbs上读到过这篇文章,并把这篇文章告诉了年长的程序员——他们都笑了。

所以我对此有两种观点,理论上的和实际的。从理论上讲,今天的Fortran与C/ c++甚至任何允许汇编代码的语言相比,并没有内在的优势。在实践中,今天的Fortran仍然享有围绕优化数值代码而建立的历史和文化遗产的好处。

Up until and including Fortran 77, language design considerations had optimization as a main focus. Due to the state of compiler theory and technology, this often meant restricting features and capability in order to give the compiler the best shot at optimizing the code. A good analogy is to think of Fortran 77 as a professional race car that sacrifices features for speed. These days compilers have gotten better across all languages and features for programmer productivity are more valued. However, there are still places where the people are mainly concerned with speed in scientific computing; these people most likely have inherited code, training and culture from people who themselves were Fortran programmers.

当人们开始谈论代码优化时,会有很多问题,了解这一点的最好方法是潜伏在那些工作是快速编写数字代码的人身上。但是请记住,这种高度敏感的代码通常只占整个代码行的一小部分,而且非常专门:许多Fortran代码就像其他语言中的许多其他代码一样“低效”,优化甚至不应该是此类代码的主要关注点。

要开始了解Fortran的历史和文化,维基百科是一个很好的地方。Fortran维基百科的条目是一流的,我非常感谢那些花时间和精力使它对Fortran社区有价值的人。

(这个答案的缩短版本本可以在Nils开始的优秀帖子中发表评论,但我没有这样做的业力。实际上,如果不是因为这个帖子有实际的信息内容和分享,而不是激烈的争吵和语言偏见,我可能根本不会写任何东西,这是我对这个主题的主要经验。我不知所措,不得不分享这份爱。)

I was doing some extensive mathematics with FORTRAN and C for a couple of years. From my own experience I can tell that FORTRAN is sometimes really better than C but not for its speed (one can make C perform as fast as FORTRAN by using appropriate coding style) but rather because of very well optimized libraries like LAPACK (which can, however, be called from C code as well, either linking against LAPACK directly or using the LAPACKE interface for C), and because of great parallelization. On my opinion, FORTRAN is really awkward to work with, and its advantages are not good enough to cancel that drawback, so now I am using C+GSL to do calculations.

在某种程度上,Fortran在设计时就考虑到了编译器优化。该语言支持整个数组操作,编译器可以利用并行性(特别是在多核处理器上)。例如,

密集矩阵乘法很简单:

matmul(a,b)

向量x的L2范数为:

sqrt(sum(x**2))

此外,FORALL、PURE和ELEMENTAL程序等语句进一步有助于优化代码。由于这个简单的原因,即使是Fortran中的指针也不如C语言灵活。

即将发布的Fortran标准(2008)具有协同数组,允许您轻松地编写并行代码。G95(开源)和来自CRAY的编译器已经支持它。

所以是的,Fortran可以很快,因为编译器可以比C/ c++更好地优化/并行化。但是就像生活中的其他事情一样,有好的编译器和坏的编译器。

没有一种语言比另一种语言更快,所以正确的答案是否定的。

你真正要问的是“用Fortran编译器X编译的代码是否比用C编译器Y编译的等效代码更快?”这个问题的答案当然取决于您选择哪两个编译器。

人们可能会问的另一个问题是“考虑到在他们的编译器中优化投入了相同的精力,哪个编译器会生成更快的代码?” 这个问题的答案实际上是Fortran。Fortran编译器有一些优势:

Fortran had to compete with Assembly back in the day when some vowed never to use compilers, so it was designed for speed. C was designed to be flexible. Fortran's niche has been number crunching. In this domain code is never fast enough. So there's always been a lot of pressure to keep the language efficient. Most of the research in compiler optimizations is done by people interested in speeding up Fortran number crunching code, so optimizing Fortran code is a much better known problem than optimizing any other compiled language, and new innovations show up in Fortran compilers first. Biggie: C encourages much more pointer use than Fortran. This drasticly increases the potential scope of any data item in a C program, which makes them far harder to optimize. Note that Ada is also way better than C in this realm, and is a much more modern OO Language than the commonly found Fortran77. If you want an OO langauge that can generate faster code than C, this is an option for you. Due again to its number-crunching niche, the customers of Fortran compilers tend to care more about optimization than the customers of C compilers.

然而,没有什么能阻止人们在C编译器的优化上投入大量精力,并使其生成比他们平台的Fortran编译器更好的代码。事实上,C编译器产生的较大销售额使得这种情况非常可行