在这个示例程序中,我以两种不同的方式做同样的事情(至少我是这么认为的)。我在我的 Linux 电脑上运行它并使用 top 监控内存使用情况。使用 gfortran 我发现第一种方式(“1”和“2”之间)使用的内存为 8.2GB,而第二种方式(“2”和“3”之间)的内存使用量为 3.0GB。使用英特尔编译器,差异更大:10GB 与 3GB。这似乎是对使用指针的过度惩罚。为什么会这样?
program test
implicit none
type nodesType
integer:: nnodes
integer,dimension(:),pointer:: nodes
end type nodesType
type nodesType2
integer:: nnodes
integer,dimension(4):: nodes
end type nodesType2
type(nodesType),dimension(:),allocatable:: FaceList
type(nodesType2),dimension(:),allocatable:: FaceList2
integer:: n,i
n = 100000000
print *, '1'
read(*,*)
allocate(FaceList(n))
do i=1,n
FaceList(i)%nnodes = 4
allocate(FaceList(i)%nodes(4))
FaceList(i)%nodes(1:4) = (/1,2,3,4/)
end do
print *, '2'
read(*,*)
do i=1,n
deallocate(FaceList(i)%nodes)
end do
deallocate(FaceList)
allocate(FaceList2(n))
do i=1,n
FaceList2(i)%nnodes = 4
FaceList2(i)%nodes(1:4) = (/1,2,3,4/)
end do
print *, '3'
read(*,*)
end program test
背景是局部网格细化。我选择了链表来轻松添加和删除面孔。默认情况下,节点数为 4,但可能会根据局部细化而变得更高。