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

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


当前回答

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

其他回答

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

你真正要问的是“用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编译器产生的较大销售额使得这种情况非常可行

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.

我还没有听说过Fortan比C快得多,但是可以想象在某些情况下它会更快。关键不在于语言特征的存在,而在于那些(通常)不存在的特征。

一个例子是C指针。C指针几乎到处都在使用,但指针的问题是编译器通常无法判断它们是否指向同一个数组的不同部分。

例如,如果你写了一个strcpy例程,看起来像这样:

strcpy(char *d, const char* s)
{
  while(*d++ = *s++);
}

编译器必须在d和s可能是重叠数组的假设下工作。所以当数组重叠时,它不能执行会产生不同结果的优化。正如您所期望的,这在很大程度上限制了可以执行的优化类型。

[我应该注意到,C99有一个“restrict”关键字,显式地告诉编译器指针不重叠。还要注意,Fortran也有指针,语义不同于C语言,但指针不像C语言那样无处不在。

但是回到C与Fortran的问题上,可以想象,Fortran编译器能够执行一些对于(直接编写的)C程序可能无法实现的优化。所以我不会对这种说法感到太惊讶。不过,我确实希望性能差异不会太大。(~ 5 - 10%)

我是一个业余程序员,在这两种语言上我都是“平均”。 我发现编写快速Fortran代码比编写C(或c++)代码更容易。Fortran和C都是“历史悠久”的语言(按照今天的标准),被大量使用,并且很好地支持免费和商业编译器。

我不知道这是否是一个历史事实,但Fortran感觉它是为并行/分布式/向量化/多核化而构建的。今天,当我们谈论速度时,它几乎是“标准度量”:“它能缩放吗?”

对于纯粹的cpu计算,我喜欢Fortran。对于任何与IO相关的东西,我发现使用c更容易(无论如何这两种情况都很困难)。

当然,对于并行计算密集型代码,你可能需要使用GPU。C和Fortran都有很多或多或少很好地集成了CUDA/OpenCL接口(现在还有OpenACC)。

我比较客观的回答是:如果你对这两种语言都同样了解或不了解,那么我认为Fortran更快,因为我发现用Fortran写并行/分布式代码比用c更容易(一旦你明白你可以写“自由形式”Fortran,而不仅仅是严格的F77代码)

Here is a 2nd answer for those willing to downvote me because they don't like the 1st answer : Both language have the features required to write high-performance code. So it's dependent of the algorithm you're implementing (cpu intensive ? io intensive ? memory intensive?), the hardware (single cpu ? multi-core ? distribute supercomputer ? GPGPU ? FPGA ?), your skill and ultimately the compiler itself. Both C and Fortran have awesome compiler. (i'm seriously amazed by how advanced Fortran compilers are but so are C compilers).

PS:我很高兴你特别排除了库,因为我有很多关于Fortran GUI库的不好的东西要说。:)

我将Fortran、C和c++的速度与netlib中的经典Levine-Callahan-Dongarra基准进行了比较。使用OpenMP的多语言版本是 http://sites.google.com/site/tprincesite/levine-callahan-dongarra-vectors C语言更丑陋,因为它一开始是自动翻译,加上某些编译器的限制和pragmas插入。 c++就是在适用的地方使用STL模板的C。在我看来,STL在是否能提高可维护性方面好坏参半。

为了了解自动函数内联在多大程度上改进了优化,只需要进行很少的练习,因为示例基于传统的Fortran实践,其中很少依赖内联。

到目前为止使用最广泛的C/ c++编译器缺乏自动向量化,而这些基准测试严重依赖于此。

关于这之前的帖子:在Fortran中使用括号来指示更快或更准确的求值顺序的例子有两个。已知的C编译器没有在不禁用更重要的优化的情况下观察括号的选项。