现代 Fortran 与现代 C++ 相比有什么优势?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4821913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:39:15  来源:igfitidea点击:

What advantages does modern Fortran have over modern C++?

c++fortranlanguage-comparisons

提问by royco

I'm trying to decide between Fortran and C++ for an application in scientific computing. It's not clear to me if Fortran still has advantages over other languages when it comes to performance. For example, I believe since Fortran enforces strict aliasing, better optimizations could be made by the compiler when compared to C before C99. I'm unsure of how C++ fits in here.

我正在尝试在 Fortran 和 C++ 之间为科学计算中的应用程序做出决定。我不清楚 Fortran 在性能方面是否仍然比其他语言具有优势。例如,我相信由于 Fortran 强制执行严格别名,与 C99 之前的 C 相比,编译器可以进行更好的优化。我不确定 C++ 是如何适应这里的。

Any guidance?

任何指导?

回答by T.E.D.

I took a look at some of the stuff in the latest Fortran standards, and frankly I'm impressed. A lot of what I hated about the language 20 years ago is gone now. No more line numbers and special columns (may they burn in hell).

我查看了最新 Fortran 标准中的一些内容,坦率地说,我印象深刻。20 年前我讨厌这门语言的很多东西现在都消失了。不再有行号和特殊列(愿它们在地狱中燃烧)。

Fortran has been heavily used in engineering circles for 50 years now. That gives you two advantages if you work in those circles. First off, these folks care a lotabout optimization. That means Fortran compilers tend to have the best optimizers around. The language itself is a lot more optimizable than Cish languages too, thanks to its lack of aliasing.

Fortran 已经在工程界大量使用了 50 年。如果您在这些圈子中工作,这会给您带来两个优势。首先,这些人非常关心优化。这意味着 Fortran 编译器往往拥有最好的优化器。由于没有别名,该语言本身也比 Cish 语言更可优化。

The second advantage is that Fortran's library support for number crunching simply cannot be beat. The best code is nearly always going to be the well-debugged code you don't have to write.

第二个优点是 Fortran 对数字运算的库支持是无与伦比的。最好的代码几乎总是你不必编写的经过良好调试的代码

If your application doesn't fall under scientific, engineering, or number crunching in general, then neither of the above will be a big deal for you, so you may be better off looking elsewhere.

如果您的应用程序一般不属于科学、工程或数字运算,那么上述任何一项对您来说都不是什么大问题,因此您最好别处寻找。

回答by user4562

The other major issue is the learning curve which is very huge for C++ and exceptionally small for Fortran (90 and later). Fortran is like MATLAB with operations like ...

另一个主要问题是学习曲线对于 C++ 来说非常大,而对于 Fortran(90 和更高版本)来说则非常小。Fortran 就像 MATLAB,具有诸如...

  • B'DB is matmul( matmul(transpose(B), D), B )
  • L2 norm of a vector is norm2(x)
  • SVD of a matrix using LAPACK is call gesvd(A,S,u,vt)
  • B'DB是 matmul( matmul(transpose(B), D), B )
  • 向量的 L2 范数是 norm2(x)
  • 使用 LAPACK 的矩阵的 SVD 是 call gesvd(A,S,u,vt)

Fortran also has pointers, dynamic memory, user defined data types etc.

Fortran 还具有指针、动态内存、用户定义的数据类型等。

It is well supported by major vendors (Intel/Sun/IBM/Cray/PGI/NAG etc.), open source (gfortan/g95) communities and developers of numerical library/APIs such as PETSc, MPI etc.

它得到了主要供应商(Intel/Sun/IBM/Cray/PGI/NAG 等)、开源(gfortan/g95)社区和数值库/API 开发人员(如 PETSc、MPI 等)的大力支持。

Heck the new standard (Fortran 2008) even has co-arrays for doing parallel programming without the need for MPI/OpenMP and some Fortran compilers already support it (g95 and Cray).

真见鬼,新标准(Fortran 2008)甚至有用于进行并行编程的协同阵列,不需要 MPI/OpenMP,并且一些 Fortran 编译器已经支持它(g95 和 Cray)。

Basically it has all the good qualities required for numerical computing, is easier than MATLAB, is standardized, free, scalable (with MPI/OpenMP and co-arrays), produces blazing fast/parallel code.

基本上它具有数值计算所需的所有优良品质,比 MATLAB 更容易,标准化、免费、可扩展(使用 MPI/OpenMP 和 co-arrays),生成极快的/并行代码。

For numerics nothing beats Fortran but unfortunately for anything else everything beats Fortan. So if you are a scientist with a safe job and only do numerical/HPC computing then stick with Fortran otherwise learn and use C++ as it is widely used for non numerical software.

对于数字,没有什么比 Fortran 好,但不幸的是,对于其他任何东西,一切都比 Fortan 好。因此,如果您是一名从事安全工作的科学家并且只进行数值/HPC 计算,那么请坚持使用 Fortran,否则请学习和使用 C++,因为它广泛用于非数值软件。

回答by Fortranner

Fortran allows whole array operations and also operations on array sections. There are C++ classes for arrays, but I don't think you can refer to a slice such as x(:,2:,1:N3:2) as easily as in Fortran. This lets one express some algorithms pretty concisely.

Fortran 允许整个数组操作以及对数组部分的操作。有用于数组的 C++ 类,但我认为您无法像在 Fortran 中那样轻松地引用 x(:,2:,1:N3:2) 这样的切片。这让我们可以非常简洁地表达一些算法。

The convenience of Fortran's array operations extends to arrays of derived types. Suppose you have a an array of dates:

Fortran 数组操作的便利性扩展到派生类型的数组。假设你有一个日期数组:

type date
integer :: month,day,year
end type date

输入日期
整数::月、日、年
结束输入日期

type(date) :: x(1000)

类型(日期):: x(1000)

Then x refers to the array of dates, x%month refers to the array of months, and pack(x,x%month==1) refers to all dates in January. How many other programming languages offer this convenience?

那么x指的是日期数组,x%month指的是月份数组,pack(x,x%month==1)指的是一月份的所有日期。有多少其他编程语言提供这种便利?

Some of the earlier comments about Fortran -- "old and disgusting" -- are biased and should be discounted accordingly. Let me argue the opposite. In my opinion the free format of Fortran 90 looks better than the syntax of C and C++, with the curly braces and semicolons. Leaving them out or incorrectly putting them in can cause errors in C and C++ that have no counterpart in Fortran.

一些早期关于 Fortran 的评论——“陈旧而恶心”——是有偏见的,应该相应地打折扣。让我反其道而行之。在我看来,Fortran 90 的自由格式看起来比带有花括号和分号的 C 和 C++ 的语法更好。将它们排除在外或错误地放入它们会导致 C 和 C++ 中出现 Fortran 中没有对应物的错误。

回答by Martin York

Fortran has been highly optimized for mathematical (especially matrix) like operations.

Fortran 已经针对数学(尤其是矩阵)之类的运算进行了高度优化。

C++ has been highly optimized for object usage.

C++ 已经针对对象使用进行了高度优化。

Which is more important to you.

哪个对你更重要。

As noted below C++ has an optimized matrix library.
But Fortran's whole purpose is optimization of mathematical processes (especially matrix operations). The fact that these optimizations are built into the foundation of the language (rather than a library) and have about a two decade head start on research over C++ I doubt (but don't know for a fact) that in this area Fortran is going to win hands down.

如下所述,C++ 有一个优化的矩阵库。
但是 Fortran 的全部目的是优化数学过程(尤其是矩阵运算)。事实上,这些优化是建立在语言的基础(而不是库)中的,并且在 C++ 研究方面领先了大约 20 年,我怀疑(但不知道事实)在这个领域 Fortran 正在走向赢得胜利。

回答by fedvasu

Advantages of fortran95 and above over c++(2003):

fortran95 及以上版本相对于 c++(2003) 的优势:

  1. As previously(by user4562) mentioned short learning curve(my first language was C and i still can't master it , similar is true for C++)
  2. (My personal opinion) Easy for code transition from Octave(for that matter Matlab)similar syntax,same modularity[I use Octave to prototype a program and rewrite in fortran95 for speed],though u can directly use octave code in C++.
  3. dynamic memory allocation is quite straight forward.(f77 didn't have this at all!)
  4. libraries support (u can do this in c++ as well , but its natural to use fortran)
  5. Co-array support for parallel computing (pity only cray supports them as of feb 2011 gfortran work has started as of gfortran4.6 but still a long way to go)
  1. 正如之前(由 user4562 提供的)提到的短学习曲线(我的第一语言是 C,但我仍然无法掌握它,C++ 也是如此)
  2. (我的个人意见)易于从 Octave(就此而言,Matlab)的代码转换类似的语法,相同的模块化[我使用 Octave 对程序进行原型设计并在 fortran95 中重写以提高速度],尽管您可以直接在 C++ 中使用八度代码。
  3. 动态内存分配非常简单。(f77 根本没有这个!)
  4. 库支持(你也可以在 c++ 中做到这一点,但使用 fortran 很自然)
  5. 对并行计算的 Co-array 支持(遗憾的是,截至 2011 年 2 月,gfortran 工作已经开始,但仍有很长的路要走)

in short if your program or application is purely scientific computation use fortran 95 and above if calculating a few numbers is just part of the story use C++ (or whatever u feel is better)

简而言之,如果您的程序或应用程序是纯粹的科学计算,请使用 fortran 95 及更高版本,如果计算一些数字只是故事的一部分,请使用 C++(或任何您觉得更好的东西)

回答by user1591744

I am new to programming.I have been programming in the field of finite elements for about a year. After some research on the net I decided to use fortran 2003.I learned to program in the modular style in about ten days by studying the Chapman book. It's one year on and i have written about four thousands of code lines in modular format(maintainable,reusable and neat codes) and haven't used any character variable at all. I don't think that by studying C++,matlab,python,java ... for ten days you would be able to write numerical codes as efficient as in fortran. fortran 2003 also has all the necessary OOP capabilities which I am learning now. So in terms of language strength in the numerical aspect fortan doesn't lack anything(modular style,OOP style,powerful array capabilities,powerful libraries, free and commercial up to dated compilers,very easy to learn, very efficient ...). Languages like python/numpy has most of these capabilities but lack efficiency. Languages like C++ also has most of the capabilities of fortran(although for array computation which is the main core of numerical computation you have to import some libraries!!), but maybe a program written by someone like me in fortran would be more efficient than one written by some c++ programmer with 10+ years of experience.
Finally I do my heavy numerical computations in fortran(modular or OOP format), and use python\numpy for small size computations(like creating plots,small size array computations ... ).

我是编程新手。我在有限元领域编程大约一年了。在网络上进行了一些研究后,我决定使用 fortran 2003。通过学习 Chapman 的书,我在大约 10 天内学​​会了模块化风格的编程。一年过去了,我已经以模块化格式(可维护、可重用和整洁的代码)编写了大约四千行代码,并且根本没有使用任何字符变量。我不认为通过学习 C++、matlab、python、java ... 十天,您将能够像在 fortran 中那样高效地编写数字代码。fortran 2003 还具有我现在正在学习的所有必要的 OOP 功能。所以在数值方面 fortan 的语言实力方面并不缺乏(模块化风格,OOP 风格,强大的数组能力,强大的库,免费和商业最新的编译器,非常容易学习,非常高效......)。像 python/numpy 这样的语言具有大部分这些功能,但缺乏效率。像C++这样的语言也拥有fortran的大部分能力(虽然对于数值计算的主要核心数组计算,你必须导入一些库!!),但也许像我这样的人用fortran编写的程序会比由一些具有 10 年以上经验的 C++ 程序员编写的。
最后,我以 fortran(模块化或 OOP 格式)进行大量数值计算,并使用 python\numpy 进行小尺寸计算(如创建绘图、小尺寸数组计算......)。

回答by user1591744

My experience with Fortran is that it's easy to learn, clean (being highly modularized), and thus well suited to a non-programmer whose main concern is doing highly optimized numerical computations. Although equal optimization can be performed in c++, (even perhaps to a greater extent), it takes a lot of instrinsic understanding to achieve those level of optimizations. When it comes to matrix calculations, out-of-the box a Fortran compiler will normally out-perform a c++ compiler. I'd also add that Fortran has keywords that are specifically designed to help the programmer to squeeze more performance out of a numerical routine. C++ has this too, but not to the extent Fortran does.

我使用 Fortran 的经验是它易于学习、干净(高度模块化),因此非常适合主要关注进行高度优化的数值计算的非程序员。尽管可以在 c++ 中执行相同的优化(甚至可能在更大程度上),但需要大量的内在理解才能实现这些级别的优化。在矩阵计算方面,开箱即用的 Fortran 编译器通常会胜过 C++ 编译器。我还要补充一点,Fortran 有专门设计用于帮助程序员从数值例程中榨取更多性能的关键字。C++ 也有这个,但没有 Fortran 那样。

Another advantage is that Fortran is not operating system or architecture specific. In other words the Fortran code you write on one operating system or architecture should easily port to another where there is a Fortran compiler.

另一个优点是 Fortran 不是特定于操作系统或体系结构的。换句话说,您在一个操作系统或体系结构上编写的 Fortran 代码应该很容易移植到另一个有 Fortran 编译器的地方。

Another advantage is that modern Fortran is normally backwards compatible with older code bases of Fortran. And the code base that has been built over the years in Fortran is huge and extremely sophisticated (being mostly done by scientists and mathematicians).

另一个优点是现代 Fortran 通常向后兼容旧的 Fortran 代码库。多年来在 Fortran 中构建的代码库非常庞大且极其复杂(主要由科学家和数学家完成)。

Also, personally, I really enjoyed the built-in file handling functions that allow one to read a data file and perform operations on it almost instantly. Many other built-in functions in Fortran are designed to allow this convenience. C++ offers mainly building blocks to do this and this requires a bit of sweating it out just to read a data file b/c you need to know something about delimiters (where Fortran allows you to specify the delimiter).

此外,就我个人而言,我非常喜欢内置的文件处理功能,它允许人们几乎立即读取数据文件并对其执行操作。Fortran 中的许多其他内置函数旨在提供这种便利。C++ 提供了主要的构建块来执行此操作,这需要花费一些精力来读取数据文件 b/c,您需要了解有关分隔符的知识(Fortran 允许您指定分隔符)。

Beyond this I can't think of advantages. Most anything else would be string manipulations, or algorithmic based operations to which c++ as a language, and in general it's compilers, are better suited and most often will perform better. A good knowledgeable programmer will likely prefer c++ as she/he would understand how to optimize a numerical routine in a way that could likely perform better than a Fortran compiled routine. Additionally good reliable Fortran compilers are not as easy to come by as good reliable C++ compilers.

除此之外,我想不出优点。大多数其他事情都是字符串操作或基于算法的操作,C++ 作为一种语言,通常它是编译器,更适合并且通常会表现得更好。一个知识渊博的程序员可能会更喜欢 C++,因为她/他会理解如何以一种可能比 Fortran 编译例程表现更好的方式优化数值例程。此外,可靠的 Fortran 编译器并不像可靠的 C++ 编译器那样容易获得。

回答by Doc Brown

IMHO, the only advantage that really matters is that programming FORTRAN allows you easier reuse of a lot of existing FORTRAN code and libraries. And if you have 50 FORTRAN programmers at hand for a project and a limited time frame, are you going first to teach them all C++, or will you accept to let them use their favorite language?

恕我直言,真正重要的唯一优势是编程 FORTRAN 可以让您更轻松地重用许多现有的 FORTRAN 代码和库。如果你手头有 50 名 FORTRAN 程序员负责一个项目并且时间有限,你会先教他们所有的 C++,还是接受让他们使用他们最喜欢的语言?

回答by Daniel Gallagher

Given the existence of scientific computing packages like LAPACK++, which are highly optimized already, modern Fortran doesn't even have a performance advantage. C++ may have its faults, but performance is not one of them.

鉴于存在已经高度优化的 LAPACK++ 等科学计算包,现代 Fortran 甚至没有性能优势。C++ 可能有它的缺点,但性能不是其中之一。

回答by sbi

With the emergence of template-meta programming (especially expression templates), C++ reached FORTRAN's league in numerical computations, so speed should not be an issueanymore. However, there's still some things to be said about other issues:

随着模板元编程(尤其是表达式模板)的出现,C++ 在数值计算方面达到了 FORTRAN 的联盟,因此速度不再是问题。但是,关于其他问题还有一些话要说:

Pro FORTRAN: The older folks might know it better than C++.
Contra FORTRAN: It's a disgusting, old, and mostly abandoned language, that's already outdatedthe moment your start your project. Whoever learns programming now, is veryunlikely to learn FORTRAN, so you might run into problems finding programmersfor the project later.

专业版 FORTRAN:年长的人可能比 C++ 更了解它。
Contra FORTRAN:这是一种令人厌恶、陈旧且几乎已被废弃的语言,从您开始项目的那一刻起就已经过时了。谁学习编程现在,是非常不可能学习FORTRAN,所以你可能会遇到寻找问题的程序员为项目后期。

Pro C++: It's relatively modern, with compilers still improving in considerable strides. It allows you to write quite expressive code.
Contra C++: Some of those template error messageswill make you weep.

Pro C++:它相对现代,编译器仍在大幅改进。它允许您编写非常有表现力的代码
Contra C++:其中一些模板错误消息会让您哭泣。