Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Efficiency of if-construct

Status
Not open for further replies.

JEichhorn

Programmer
Oct 21, 2014
10
0
0
DE
I was wondering, which of the following code snippets would usually perform faster.

First avoiding IF statements within the loop, at the cost of a few additional arithmetic operations:
Code:
   DO k = 2,n-2
   DO i = 2,l-2
      vvb          = sign(0.5,v(i,2,k))
      zwil1(i,1,k) = (0.5+vvb)*eps(i,1,k) - (vvb-0.5)*zwil1(i,2,k)
   END DO
   END DO

Second using IF blocks:
Code:
   DO k = 2,n-2
   DO i = 2,l-2
      IF (v(i,2,k) .LT. 0.0) THEN
         zwil1(i,1,k)   = zwil1(i,2,k)
      ELSE
         zwil1(i,1,k)   = eps(i,1,k)
      END IF
   END DO
   END DO

Of course i could just try out on one machine at least, but nevertheless any comments on this would be appreciated.
 
It depends on whether the implementation of SIGN has jumps in it. On some machine architectures, it is a built in instruction so there won't be any jumps. If SIGN has jumps, you're just trading one set of jumps for another.
 
The way I see it, the second loop structure would be much faster...first of all, I thought IF evaluations were pretty fast; second, the second loop does not have multiplications or summations, it is just a straight out assignment.
 
I have checked both versions on a quadcore i7 both under Windows 8.1 and Ubuntu 14.04 LTS, compiled with Absoft 14.0. For a problem consuming ~10 minutes of CPU time, the difference between both versions was less than a few seconds. So i will stay with the IF blocks with their superior readability.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top