我不时地读到Fortran在繁重的计算中比C更快。这是真的吗?我必须承认我几乎不懂Fortran,但是到目前为止我看到的Fortran代码并没有显示出该语言具有C语言所不具备的特性。
如果是真的,请告诉我原因。请不要告诉我什么语言或库适合处理数字,我不打算写一个应用程序或库来做这个,我只是好奇。
我不时地读到Fortran在繁重的计算中比C更快。这是真的吗?我必须承认我几乎不懂Fortran,但是到目前为止我看到的Fortran代码并没有显示出该语言具有C语言所不具备的特性。
如果是真的,请告诉我原因。请不要告诉我什么语言或库适合处理数字,我不打算写一个应用程序或库来做这个,我只是好奇。
当前回答
Fortran有更好的I/O例程,例如隐含的do工具提供了C标准库无法比拟的灵活性。
Fortran编译器直接处理更复杂的 涉及到语法,而且这样的语法不能轻易简化 参数传递形式,C不能有效地实现它。
其他回答
There is another item where Fortran is different than C - and potentially faster. Fortran has better optimization rules than C. In Fortran, the evaluation order of an expressions is not defined, which allows the compiler to optimize it - if one wants to force a certain order, one has to use parentheses. In C the order is much stricter, but with "-fast" options, they are more relaxed and "(...)" are also ignored. I think Fortran has a way which lies nicely in the middle. (Well, IEEE makes the live more difficult as certain evaluation-order changes require that no overflows occur, which either has to be ignored or hampers the evaluation).
另一个更聪明的规则领域是复数。这不仅是因为直到c99才有了它们,而且Fortran中管理它们的规则更好;由于gfortran的Fortran库部分是用C编写的,但实现了Fortran语义,GCC获得了这个选项(也可以用于“普通”C程序):
-fcx-fortran-rules 复杂的乘法和除法遵循Fortran规则。范围缩减是作为复杂除法的一部分进行的,但是没有检查复杂乘法或除法的结果是否是“NaN + I*NaN”,试图在这种情况下挽救这种情况。
The alias rules mentioned above is another bonus and also - at least in principle - the whole-array operations, which if taken properly into account by the optimizer of the compiler, can lead faster code. On the contra side are that certain operation take more time, e.g. if one does an assignment to an allocatable array, there are lots of checks necessary (reallocate? [Fortran 2003 feature], has the array strides, etc.), which make the simple operation more complex behind the scenes - and thus slower, but makes the language more powerful. On the other hand, the array operations with flexible bounds and strides makes it easier to write code - and the compiler is usually better optimizing code than a user.
总的来说,我认为C和Fortran的速度差不多;选择应该更多的是你更喜欢哪种语言,或者是使用Fortran的全数组操作及其更好的可移植性更有用,还是使用C中更好的系统接口和图形用户界面库。
使用现代标准和编译器,不!
Some of the folks here have suggested that FORTRAN is faster because the compiler doesn't need to worry about aliasing (and hence can make more assumptions during optimisation). However, this has been dealt with in C since the C99 (I think) standard with the inclusion of the restrict keyword. Which basically tells the compiler, that within a give scope, the pointer is not aliased. Furthermore C enables proper pointer arithmetic, where things like aliasing can be very useful in terms of performance and resource allocation. Although I think more recent version of FORTRAN enable the use of "proper" pointers.
对于现代实现,C通用优于FORTRAN(尽管它也非常快)。
http://benchmarksgame.alioth.debian.org/u64q/fortran.html
编辑:
一个公平的批评似乎是,基准测试可能是有偏见的。这里是另一个来源(相对于C),将结果放在更多的上下文中:
http://julialang.org/benchmarks/
你可以看到C在大多数情况下优于Fortran(再次看到下面的批评也适用于这里);正如其他人所指出的,基准测试是一门不精确的科学,很容易偏袒一种语言而不是其他语言。但它确实说明了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.
简单快捷: 这两种语言同样快,但Fortran更简单。 到底哪个更快取决于算法,但无论如何,速度上没有很大的差别。这是我2015年在德国斯图加德高性能计算中心的Fortran研讨会上所学到的。我同时使用Fortran和C语言,我也有同样的观点。
解释:
C语言是用来编写操作系统的。因此,它拥有编写高性能代码所需的更多自由。一般来说,这是没有问题的,但是如果一个人不仔细编程,他很容易减慢代码的速度。
Fortran是为科学编程而设计的。因此,它支持编写语法方面的快速代码,因为这是Fortran的主要目的。与公众的看法相反,Fortran并不是一种过时的编程语言。它的最新标准是2010年,新的编译器定期发布,因为大多数高性能代码都是用Fortran编写的。Fortran进一步支持现代功能,如编译器指令(在C语言中)。
例子: 我们想给一个大的结构体作为函数的输入参数(fortran: suboutine)。在函数中,参数不会被改变。
C同时支持引用调用和值调用,这是一个非常方便的特性。在我们的例子中,程序员可能会意外地使用按值调用。这大大降低了速度,因为需要首先将结构体复制到内存中。
Fortran只使用引用调用,这迫使程序员手动复制结构,如果他真的想要按值调用操作。在我们的例子中,通过引用调用,fortran将自动和C版本一样快。
我是一个业余程序员,在这两种语言上我都是“平均”。 我发现编写快速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库的不好的东西要说。:)