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

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


当前回答

This is more than somewhat subjective, because it gets into the quality of compilers and such more than anything else. However, to more directly answer your question, speaking from a language/compiler standpoint there is nothing about Fortran over C that is going to make it inherently faster or better than C. If you are doing heavy math operations, it will come down to the quality of the compiler, the skill of the programmer in each language and the intrinsic math support libraries that support those operations to ultimately determine which is going to be faster for a given implementation.

编辑:@Nils等人提出了一个很好的观点,即C语言中指针使用的差异,以及可能存在的别名,这可能会使C语言中最简单的实现变慢。然而,在C99中有一些方法可以解决这个问题,比如通过编译器优化标志和/或C语言的实际编写方式。这在@Nils的回答和随后的评论中有很好的介绍。

其他回答

This is more than somewhat subjective, because it gets into the quality of compilers and such more than anything else. However, to more directly answer your question, speaking from a language/compiler standpoint there is nothing about Fortran over C that is going to make it inherently faster or better than C. If you are doing heavy math operations, it will come down to the quality of the compiler, the skill of the programmer in each language and the intrinsic math support libraries that support those operations to ultimately determine which is going to be faster for a given implementation.

编辑:@Nils等人提出了一个很好的观点,即C语言中指针使用的差异,以及可能存在的别名,这可能会使C语言中最简单的实现变慢。然而,在C99中有一些方法可以解决这个问题,比如通过编译器优化标志和/或C语言的实际编写方式。这在@Nils的回答和随后的评论中有很好的介绍。

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

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

一般来说,FORTRAN比C慢。C可以使用硬件级指针,允许程序员手动优化。FORTRAN(在大多数情况下)不能访问硬件内存寻址黑客。(VAX FORTRAN是另一回事。)我从70年代开始断断续续地使用FORTRAN。(真的)。

然而,从90年代开始,FORTRAN已经发展到包括特定的语言结构,可以优化成内在的并行算法,真正可以在多核处理器上运行。例如,自动矢量化允许多个处理器同时处理数据向量中的每个元素。16个处理器——16个元素向量——处理需要1/16的时间。

在C语言中,您必须管理自己的线程并为多处理仔细设计算法,然后使用一堆API调用来确保并行性正确发生。

在FORTRAN中,您只需要为多处理仔细设计算法。编译器和运行时可以为您处理其余的工作。

您可以阅读一些关于高性能Fortran的内容,但是您会发现许多死链接。你最好阅读并行编程(比如OpenMP.org)以及FORTRAN如何支持并行编程。

I think the key point in favor of Fortran is that it is a language slightly more suited for expressing vector- and array-based math. The pointer analysis issue pointed out above is real in practice, since portable code cannot really assume that you can tell a compiler something. There is ALWAYS an advantage to expression computaitons in a manner closer to how the domain looks. C does not really have arrays at all, if you look closely, just something that kind of behaves like it. Fortran has real arrawys. Which makes it easier to compile for certain types of algorithms especially for parallel machines.

在运行时系统和调用约定等方面,C语言和现代的Fortran非常相似,很难看出有什么不同。注意,这里的C实际上是基础C: 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开始的优秀帖子中发表评论,但我没有这样做的业力。实际上,如果不是因为这个帖子有实际的信息内容和分享,而不是激烈的争吵和语言偏见,我可能根本不会写任何东西,这是我对这个主题的主要经验。我不知所措,不得不分享这份爱。)