what's the advantages of fortran pointer over allocatable

what's the advantages of fortran pointer over allocatable

Recently, I am studying the efficiency difference in using pointer and allocatable variables. 

I have tested the character type, but I could not prove that the allocatable is more efficient than the pointer as per http://software.intel.com/en-us/forums/topic/276711

What I could get is opposite. Has anybody experienced the same?

8 帖子 / 0 全新
最新文章
如需更全面地了解编译器优化,请参阅优化注意事项

I very much doubt you'd find POINTER has an advantage over ALLOCATABLE. ALLOCATABLE has the advantage that the compiler knows the data is contiguous, allowing it to skip making temporary copies of the data.  ALLOCATABLE also makes it very difficult if not impossible to leak memory. Use ALLOCATABLE unless you must use pointer assignment.

Steve - Intel Developer Support

It would be nice if more examples can be proving...

I doubt myself also. But I tested the example on two machines, one desktop and one laptop and I got the same results.

Please help with further testing.

Thanks, Kong

附件: 

附件尺寸
下载 pointer.f903.3 KB
下载 test-readline.txt279.53 KB
下载 allocatable.f903.42 KB

My results in win32 release mode are:

Allocatable string Running Time was:  0.1133607     [Min]

Pointer string Running Time was:  5.6420636E-02 [Min]

I re-implemented the reallocate function as a subroutine and got: Allocatable string Running Time was:  5.7720371E-02 [Min].

subroutine reallocate(P,N)
CHARACTER(len=:),allocatable,intent(inout)::P
INTEGER,INTENT(IN)::N
CHARACTER(len=:),allocatable::R
! reallocate=repeat(' ',N) ! should avoid repeat function which consumes too much RAM
allocate(character(n)::R); R(:n)=' ';
IF(ALLOCATED(P))then
   NOLD=len(P)
   R(1:MIN(NOLD,N))=P(1:MIN(NOLD,N))
   DEALLOCATE(P)
end if
call move_alloc(R, P)
END subroutine

Without measuring to confirm, I would have though the IO in the code would have dominated over any memory management strategy.

Thanks a lot. So the finding is that the function is much less effecient than the subroutine. Unfortunately, for years, I have converted subroutines into functions to simulate c style...

I agree with IanH, but could we have some substantially better alternatives?

发表评论

登录添加评论。还不是成员?立即加入