我不时地读到Fortran在繁重的计算中比C更快。这是真的吗?我必须承认我几乎不懂Fortran,但是到目前为止我看到的Fortran代码并没有显示出该语言具有C语言所不具备的特性。
如果是真的,请告诉我原因。请不要告诉我什么语言或库适合处理数字,我不打算写一个应用程序或库来做这个,我只是好奇。
我不时地读到Fortran在繁重的计算中比C更快。这是真的吗?我必须承认我几乎不懂Fortran,但是到目前为止我看到的Fortran代码并没有显示出该语言具有C语言所不具备的特性。
如果是真的,请告诉我原因。请不要告诉我什么语言或库适合处理数字,我不打算写一个应用程序或库来做这个,我只是好奇。
当前回答
Fortran速度更快有几个原因。然而,它们的重要性是如此无关紧要,或者可以通过任何方式解决,所以它不应该是重要的。现在使用Fortran的主要原因是维护或扩展遗留应用程序。
PURE and ELEMENTAL keywords on functions. These are functions that have no side effects. This allows optimizations in certain cases where the compiler knows the same function will be called with the same values. Note: GCC implements "pure" as an extension to the language. Other compilers may as well. Inter-module analysis can also perform this optimization but it is difficult. standard set of functions that deal with arrays, not individual elements. Stuff like sin(), log(), sqrt() take arrays instead of scalars. This makes it easier to optimize the routine. Auto-vectorization gives the same benefits in most cases if these functions are inline or builtins Builtin complex type. In theory this could allow the compiler to reorder or eliminate certain instructions in certain cases, but likely you'd see the same benefit with the struct { double re; double im; }; idiom used in C. It makes for faster development though as operators work on complex types in Fortran.
其他回答
大多数帖子已经提出了令人信服的论点,所以我只是在另一个方面加上众所周知的2美分。
在处理能力方面,fortran更快或更慢是有其重要性的,但如果用fortran开发一些东西需要5倍多的时间,因为:
it lacks any good library for tasks different from pure number crunching it lack any decent tool for documentation and unit testing it's a language with very low expressivity, skyrocketing the number of lines of code. it has a very poor handling of strings it has an inane amount of issues among different compilers and architectures driving you crazy. it has a very poor IO strategy (READ/WRITE of sequential files. Yes, random access files exist but did you ever see them used?) it does not encourage good development practices, modularization. effective lack of a fully standard, fully compliant opensource compiler (both gfortran and g95 do not support everything) very poor interoperability with C (mangling: one underscore, two underscores, no underscore, in general one underscore but two if there's another underscore. and just let not delve into COMMON blocks...)
那么这个问题就无关紧要了。如果某样东西很慢,大多数时候你无法在给定的限制范围内改进它。如果你想要更快,改变算法。最后,使用电脑的时间很便宜。人类的时间不是。珍惜减少人类时间的选择。如果它增加了使用电脑的时间,无论如何它都是有成本效益的。
Fortran有更好的I/O例程,例如隐含的do工具提供了C标准库无法比拟的灵活性。
Fortran编译器直接处理更复杂的 涉及到语法,而且这样的语法不能轻易简化 参数传递形式,C不能有效地实现它。
Fortran traditionally doesn't set options such as -fp:strict (which ifort requires to enable some of the features in USE IEEE_arithmetic, a part of f2003 standard). Intel C++ also doesn't set -fp:strict as a default, but that is required for ERRNO handling, for example, and other C++ compilers don't make it convenient to turn off ERRNO or gain optimizations such as simd reduction. gcc and g++ have required me to set up Makefile to avoid using the dangerous combination -O3 -ffast-math -fopenmp -march=native. Other than these issues, this question about relative performance gets more nit-picky and dependent on local rules about choice of compilers and options.
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++是一个完全不同的问题,具有非常不同的性能特征。
The faster code is not really up to the language, is the compiler so you can see the ms-vb "compiler" that generates bloated, slower and redundant object code that is tied together inside an ".exe", but powerBasic generates too way better code. Object code made by a C and C++ compilers is generated in some phases (at least 2) but by design most Fortran compilers have at least 5 phases including high-level optimizations so by design Fortran will always have the capability to generate highly optimized code. So at the end is the compiler not the language you should ask for, the best compiler i know is the Intel Fortran Compiler because you can get it on LINUX and Windows and you can use VS as the IDE, if you're looking for a cheap tigh compiler you can always relay on OpenWatcom.
更多信息: http://ed-thelen.org/1401Project/1401-IBM-Systems-Journal-FORTRAN.html