- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I often use parameters to enable or disable parts of my code. I want to make sure that I am doing it the most efficient way. e.g. if I have.
integer, parameter :: dosomething = 0
print *, y+dosomething*somefunction(x)
then would the compiler internally completely remove dosomething*somefunction(x) so treating it just the same as if I had just written print *, y? How, if at all, does this behaviour depend on the optimization settings? Would it be better to use conditional statements?
thank you for your time
integer, parameter :: dosomething = 0
print *, y+dosomething*somefunction(x)
then would the compiler internally completely remove dosomething*somefunction(x) so treating it just the same as if I had just written print *, y? How, if at all, does this behaviour depend on the optimization settings? Would it be better to use conditional statements?
thank you for your time
Link Copied
3 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
If somefunction were declared PURE, in principle, the compiler could do this. I doubt that it is sufficiently common practice for a compiler to be designed with this in mind.
logical, parameter :: dosomething = .false.
print *, merge(y+somefunction(x),y,dosomething)
for example, looks more direct to me, but would not solve the problem.
logical, parameter :: dosomething = .false.
print *, merge(y+somefunction(x),y,dosomething)
for example, looks more direct to me, but would not solve the problem.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks. I tried a simple piece of code:
program test
implicit none
integer, parameter :: dosomething = 0
real(8), external :: somefunction
integer :: i
do i = 0, 5
print *, 5.0_8 + dosomething*somefunction(i)
end do
end program
function somefunction(x)
implicit none
integer, intent(in) :: x
real(8) :: somefunction
somefunction = 1.0_8/dble(x)
end function
Now, if I compile this without any optimizations (O0), then the first thing that is printed is NAN since we have zero times infinity so the compiler is clearly not removing it. If I compile it with higher level optimizations, then it just prints 5 so it looks like the compiler is completely removing the code in that case. Moreover, if I compare the binary files with optimizations, then they are identical regardless of whether I have dosomething=0 or whether dosomething*somefunction(i) is commented out. Does anyone know if the compiler will always do this?
program test
implicit none
integer, parameter :: dosomething = 0
real(8), external :: somefunction
integer :: i
do i = 0, 5
print *, 5.0_8 + dosomething*somefunction(i)
end do
end program
function somefunction(x)
implicit none
integer, intent(in) :: x
real(8) :: somefunction
somefunction = 1.0_8/dble(x)
end function
Now, if I compile this without any optimizations (O0), then the first thing that is printed is NAN since we have zero times infinity so the compiler is clearly not removing it. If I compile it with higher level optimizations, then it just prints 5 so it looks like the compiler is completely removing the code in that case. Moreover, if I compare the binary files with optimizations, then they are identical regardless of whether I have dosomething=0 or whether dosomething*somefunction(i) is commented out. Does anyone know if the compiler will always do this?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
"always" is perhaps a bit too strong. The compiler does some interprocedural optimization by default at level O2 or higher and in cases where it can see that a computation is not required, it may remove it or evaluate it at compile time.

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page