在Fortran 90中使用2D数组与派生类型的数组 [英] Using 2d array vs array of derived type in Fortran 90

查看:120
本文介绍了在Fortran 90中使用2D数组与派生类型的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您需要一个数组列表,每个数组具有相同的大小。使用2D数组在性能上是否更好:

Assuming you want a list of arrays, each having the same size. Is it better performance-wise to use a 2D array :

integer, allocatable :: data(:,:)

或一系列派生类型:

type test
    integer, allocatable :: content(:)
end type
type(test), allocatable :: data(:)

当然,对于不同大小的数组,我们别无选择。但是在两种情况下如何管理内存?另外,其中之一是良好的代码习惯吗?

Of course, for arrays of different sizes, we don't have a choice. But how is the memory managed between the 2 cases ? Also, is one of them good code practice ?

推荐答案

通常,您想使用适合您问题的最简单数据结构。如果二维矩形阵列满足您的需要-并且对于大量科学计算问题,Fortran是一个很好的选择,那么它确实会-那么,这就是您想要的选择。

In general, you want to use the simplest data structure that suits your problem. If a 2d rectangular array meets your needs - and for a huge number of scientific computing problems, problems for which Fortran is a good choice, it does - then that's the choice you want.

2d数组在内存中是连续的,由于缓存和较少的间接级别,这通常会使访问它更快。 2d数组还将允许您执行 data = data * 2 data = 0。这样的操作-of-array方法不会 。这些优点足够强大,即使您有参差不齐的数组,即使预期的行长范围不是那么大,有时也应考虑将其实现为矩形2d数组。

The 2d array will be contiguous in memory, which will normally make accessing it faster both due to caching and one fewer level of indirection; the 2d array will also allow you to do things like data = data * 2 or data = 0. which the array-of-array approach doesn't . Those advantages are great enough that even when you have "ragged arrays", if the range of expected row lengths isn't that large, implementing it as a rectangular 2d array is sometimes a choice worth considering.

这篇关于在Fortran 90中使用2D数组与派生类型的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆