For Programmers: Free Programming Magazines  


Home > Archive > Fortran > April 2006 > Suggestion for a faster code









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Suggestion for a faster code
lello

2006-04-13, 7:03 pm

Hi,
I am developing a code in which I need to generate a sequence of
arrays,
that is suppose I have the integer array

z = (zmax1, zmax2, ..., zmaxn)

where zmax1,...,zmaxn a constat integers,
and I want to generate the arrays

(1,0,...,0) , (2,0,...,0) , .... , (zmax1,0,...,0)
(0,1,...,0) , (1,1,...,0) , .... , (zmax1,zmax2,...,0)

and so on. I am currently using a piece of code like the following

do i = 1, nmax
if (z(i) < zmax(i)) then
z(i) = z(i) + 1
if (i > 1) z(1:i-1) = 0
exit
end if
end do

This is put inside a do loop and called millions of times and I was
wondering if anyone of you
can think of a faster way to write the code.

Lello.

Michael Metcalf

2006-04-14, 8:04 am


"lello" <rborrelli@gmail.com> wrote in message
news:1144952070.386500.221700@g10g2000cwb.googlegroups.com...
>
> if (i > 1) z(1:i-1) = 0


Well, for starters, the if(i>1) is redundant. If the condition is false,
z(1:0) is a zero-sized array and the assignment becomes a no-op.The compiler
is responsible for sorting that out (see also "Fortran 95/2003 Explained",
Section 6.1).

Regards,

Mike Metcalf


lello

2006-04-14, 7:05 pm

Thanks, for the suggestion. I knew that, but I didn't use it since I
thought (probably without any true reason) it could give some problems
with certain compilers, but I am going to switch
to your suggestion.
Any other possible speed up?

Thank you very much,
Lello.

Michael Metcalf

2006-04-14, 7:05 pm


"lello" <rborrelli@gmail.com> wrote in message
news:1145026145.845640.304170@z34g2000cwc.googlegroups.com...
> Thanks, for the suggestion. I knew that, but I didn't use it since I
> thought (probably without any true reason) it could give some problems
> with certain compilers, but I am going to switch
> to your suggestion.
> Any other possible speed up?
>

Could you submit a small complete test program?

Mike Metcalf


lello

2006-04-14, 7:05 pm

My code is quite long. I have tried to write some that resemble mine.
By the way in the code below, which is different from the one I have
sketeched before,
the statement if (i>1) cannot be eliminated.
The arrays zmax, and index are actually determined during the execution
of the program,
as well as their size nex.

program nexvib

integer n, i, nex, r, mu, NFC
integer, allocatable :: z(:), zmax(:), index(:)

NFC = 100000000
n = 100
nex = 5

allocate(z(1:n),zmax(1:n),index(1:nex))
index = (/ 3, 5, 55, 60, 90 /)
zmax = 0
zmax(index(:)) = (/10, 13, 40, 35, 25/); ! This is just an example.

do r = 1, NFC

do i = 1, nex
mu = index(i)
if (z(mu) < zmax(mu)) then
z(mu) = z(mu) + 1
if( i> 1) z(1:index(i-1)) = 0
exit
end if
end do

! Here there is other code, but it is quite fast.

end do

end program nexvib

Thanks again.
Lello.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com