real, dimension(1000,1000):: A, B, Tmp Tmp=A; A=B; B=Tmp ! 3 array copies involvedIf we decide to work with pointers to arrays in the code swapping will only involve changing pointer values, which is considerably more efficient:
real, dimension(1000,1000) :: mat1, mat2 real, dimension(:,:), pointer :: a, b, tmp a=>mat1; b=> mat2 ! initial tmp=>a; ! tmp points to the target of a, i.e. to mat1 a=>b; ! a points to the target of b, i.e. to mat2 b=>tmp ! b points to the target of tmp, i.e. to mat1or even better:
real, dimension(1000,1000), target :: MatA, MatB real, dimension(:,:), pointer :: A, A A=>MatA; B=>MatB ! normal A=>MatB; B=>MatA ! swapped