|
[Sponsors] |
June 13, 2006, 23:29 |
which is a better way to loop in fortran?
|
#1 |
Guest
Posts: n/a
|
hi,
i've a question regarding looping. I have thought of 2 types of looping but I'm not very sure which is better. Hope some programming expert can enlighten me... I'm using fortran, but I think it should apply to most languages. For e.g. case 1: if x=1 if y=1 ... else if y/=1 ... end if else if y=1 ... else if y/=1 ... end if end if case 2: if x=1 and y=1 ... else if x=1 and y/=1 ... else if x/=1 and y/=1 ... else if x/=1 and y=1 ... end if I have similar problem but with more conditions. So is one definitely better than the other? which one then? Thanks! |
|
June 14, 2006, 04:35 |
Re: which is a better way to loop in fortran?
|
#2 |
Guest
Posts: n/a
|
Since you appear to be using f90 (i.e. your using /= for .ne.) why not see if you can use the case statement:
select case (expr) case (1) ..... case (2) ..... ..... end select The other option may be the where/elsewhere structure. In practice you want to avoid if tests within do loops if you can. |
|
June 15, 2006, 01:27 |
Re: which is a better way to loop in fortran?
|
#3 |
Guest
Posts: n/a
|
tks for your suggestion. does it mean that for my case 2 e.g., it has to go thru 1 by 1 to test for the condition (min 1 time, max 4 times).
however, if i use the case statement, after evaluating the case(expr), it 'll jump immediately to the correct area. is that so or will it also do condition testing? tks again. |
|
June 15, 2006, 04:49 |
Re: which is a better way to loop in fortran?
|
#4 |
Guest
Posts: n/a
|
If set up correctly it should jump to directly to correct piece of code (although in most cases it will need to check against each condition separately - the point of case statements is to make the code easier to read compared to the equilavent nested if tests).
you really need to look at why you're doing this inside of a loop though - it tends to make the code rather inefficient. |
|
June 15, 2006, 09:38 |
Re: which is a better way to loop in fortran?
|
#5 |
Guest
Posts: n/a
|
Hi,
Using conditions inside loops is not efficient at all. On some architectures it will inhibate many compiling optimizations. Use it only when you have no other choice. It means you might rewrite your code maybe more crudely to explicitely treat some special areas of your computational domain (e.g. boundary conditions), or sometimes use step functions (in fortran, with min and max functions) to avoid IF conditions. Depends on your compiler, architecture, and code requirements ... Regards |
|
June 15, 2006, 11:15 |
Re: which is a better way to loop in fortran?
|
#6 |
Guest
Posts: n/a
|
I would say that efficiency or inefficiency is a bit relative... how many steps does your loop have? Are you using this loop within an iterative process, i.e., are you calling this loop many times or you only need to use it once?
Of course that branching statements or subroutine callings within do loops are not a good idea but if you only need to use such statements few times... no problem... Regards Renato. |
|
June 23, 2006, 05:26 |
Re: which is a better way to loop in fortran?
|
#7 |
Guest
Posts: n/a
|
i've done 2 types of looping:
1. do j=1,10 do i=1,10 if (i>5) then a(i,j)=2*i*j else a(i,j)=10*i*j end if end do end do 2. t(1:5)=1 t(6:10)=0 do j=1,10 do i=1,10 a(i,j)=2*i*j*t(i)+10*i*j*(1-t(i)) end if end do end do both should give same results, with the 2nd one has no "if" inside the loop. I tried to use the profiler for a large sample size but the results are quite inconisistant, probably due to the caching. But is the second method going to be faster? tks |
|
June 23, 2006, 05:47 |
Re: which is a better way to loop in fortran?
|
#8 |
Guest
Posts: n/a
|
Not necessarily. As Renato said it all depends on how many times this bit of code is used (the small value of 10 means that it'll run quickly anyway).
Two better ways of writing your example code are do j=1,10 do i=1,5 a(i,j)=2*i*j enddo do i=6,10 a(i,j)=10*i*j enddo enddo or do j=1,10 do i=1,5 a(i,j)=2*i*j a(i+5,j)=10*(i+5)*j enddo enddo |
|
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
[Gmsh] Problem with Gmsh | nishant_hull | OpenFOAM Meshing & Mesh Conversion | 23 | August 5, 2015 03:09 |
[CAD formats] my stl surface is seen as just a line | rcastilla | OpenFOAM Meshing & Mesh Conversion | 2 | January 6, 2010 02:30 |
NACA0012 geometry/design software needed | Franny | Main CFD Forum | 13 | July 7, 2007 16:57 |
Re: which is a better way to loop in fortran? | zonexo | Main CFD Forum | 3 | June 25, 2006 16:19 |
Big loop problem of fortran | Wen Long | Main CFD Forum | 9 | April 1, 2004 13:38 |