Replace GoTo with Do While

mms
09-02-2003, 07:33 PM
I am simply trying to re-write the following
using a Do While structure.

TOP:
If k% > j% Then GoTo BOTTOM
j% = j% - k%
k% = k% / 2
GoTo TOP
BOTTOM:

Nothing I've tried works.

Can anyone help?

MikeJ
09-02-2003, 07:43 PM
Have you tried:

Do While k% < j%
j% = j% - 2
k% = k% / 2
Loop

:confused:
~Mike

mms
09-02-2003, 07:56 PM
Yes, well actually:

Do While k% <= j%
j% = j% - 2
k% = k% / 2
Loop

Still not working.
:confused:

gfleming
09-02-2003, 08:03 PM
Hi there,

Not sure what problem with your code is, except the code in the original question has:

j% = j% - k%

Then both solutions change that to

j% = j% - 2


Apart from that, the suggested code seems fine. What are the variables, what do they represent and how are they formatted?

Garth.


LoopI am simply trying to re-write the following
using a Do While structure.

TOP:
If k% > j% Then GoTo BOTTOM
j% = j% - k%
k% = k% / 2
GoTo TOP
BOTTOM:

Nothing I've tried works.

Can anyone help?

mms
09-02-2003, 08:17 PM
Actually, what I tried was:

Do While k% <= j%
j% = j% - k%
k% = k% / 2
Loop

I did not have j% = j% - 2

Testing this further I find that the above does work
however
the routine takes 16 seconds to execute, whereas the
original way takes less than 1 second
(I thought the loop was hanging)

Now that I find it does work, I can't understand why it is so
inefficient.

I was trying to re-write this part because I read that using GoTo's is
poor programming practice.

:confused:

OnErr0r
09-02-2003, 09:21 PM
Switch to integer division, which is much faster (since your variables are integers anyway):


Dim k As Integer
Dim j As Integer

Do While k <= j
j = j - k
k = k \ 2
Loop


Just curious, what kind of values for j and k are you using to take so long?

mms
09-02-2003, 09:53 PM
I will take your suggestion about integer division for speed.

Actually that was exactly what I was trying to do (optimize code
for speed).

The code takes ~0.9 sec to execute (I found the error causing
16 sec execution time :whoops: )

Anyways I can speed it up to ~0.5 seconds if I change 4 Dim
statements to a Long instead of a Variant.

Problem is I get slightly different data back, and I am not
sure which returned data is more correct (or if it actually makes
a difference).

8192 are maximum values for J and K

OnErr0r
09-02-2003, 11:10 PM
The original code used Integer (%), so I did as well. Long is usually preferable unless you have a specific reason to use another data type. I usually only use variant as a last resort.

8192 for max.. hrm. Is there a specific value you're using for j and k for this test? If so, what exactly are they.

mms
09-03-2003, 11:03 AM
The routine above is part of a larger routine I am trying to optimize.

For my test I set N = 16384, thus giving J & K initial values of 8192.

Function FFT(N As Integer, REX() As Double, IMX() As Double)

Dim I As Integer
Dim IP As Integer
Dim J As Integer
Dim JM1 As Integer
Dim K As Integer
Dim L As Integer
Dim LE As Integer
Dim LE2 As Integer
Dim M As Integer
Dim NM1 As Integer
Dim ND2 As Integer

Dim TR As Double 'Long
Dim TI As Double 'Long
Dim UR As Double 'Long
Dim UI As Double 'Long
Dim SR As Double 'Long
Dim SI As Double 'Long

Const PI = 3.14159265
NM1 = N - 1
ND2 = N \ 2 'N / 2
M = CInt(Log(N) / Log(2))
J = ND2

For I = 1 To N - 2
If I < J Then
TR = REX(J)
TI = IMX(J)
REX(J) = REX(I)
IMX(J) = IMX(I)
REX(I) = TR
IMX(I) = TI
End If
K = ND2
Do While K <= J
J = J - K
K = K \ 2 'K / 2
Loop
J = J + K
Next I

For L = 1 To M
LE = CInt(2 ^ L)
LE2 = LE \ 2 'LE / 2
UR = 1
UI = 0
SR = Cos(PI / LE2)
SI = -Sin(PI / LE2)
For J = 1 To LE2
JM1 = J - 1
For I = JM1 To NM1 Step LE
IP = I + LE2
TR = REX(IP) * UR - IMX(IP) * UI
TI = REX(IP) * UI + IMX(IP) * UR
REX(IP) = REX(I) - TR
IMX(IP) = IMX(I) - TI
REX(I) = REX(I) + TR
IMX(I) = IMX(I) + TI
Next I
TR = UR
UR = TR * SR - UI * SI
UI = TR * SI + UI * SR
Next J
Next L

End Function

passel
09-03-2003, 11:42 AM
I guess the code must insure that k is greater than 1/2 j, otherwise
k will reach 0 before j, and you will be stuck in an endless loop.

I would think that a check before the loop to insure the k > (j\2)+2
would be necessary as a safety check, but maybe that case won't
exist.

SR = Cos(PI / LE2)
SI = -Sin(PI / LE2)
You could put PI/LE2 in a variable, so you only do the divide once each
pass (small savings, but simple)

mms
09-03-2003, 12:51 PM
I guess the code must insure that k is greater than 1/2 j, otherwise
k will reach 0 before j, and you will be stuck in an endless loop.

I would think that a check before the loop to insure the k > (j\2)+2
would be necessary as a safety check, but maybe that case won't
exist.

I'll have to think about those possibilities.

SR = Cos(PI / LE2)
SI = -Sin(PI / LE2)
You could put PI/LE2 in a variable, so you only do the divide once each
pass (small savings, but simple)

Being time critical, every little bit helps!

What about Dimming all Integers as Long?

passel
09-03-2003, 01:08 PM
Yes, that should help. Since the processor is a 32-bit machine, using
16 bits actually slows it down a little.

mms
09-03-2003, 01:19 PM
OK. thanks!

Similiar Fortran routines to this one make use of a Complex
arrays that handle complex numbers (ie Real and Imaginary parts),
does VB have this type, or can they (complex numbers) be handled in another way?

Also textbooks talk of using look-up tables to retrieve values
for SR and SI instead of calculating them.
Does that make sense in this case?
If yes, how would you go about implementing look-up tables?

passel
09-03-2003, 01:28 PM
I'm no autority on FastFourierTransforms. This link has links that
contain a fair amount of info, and there is Visual Basic Source for
FFT that says it uses the two arrays, 1 real, and 1 imaginary. The
imaginary array just uses the Double Type, so I don't really know what
makes the numbers "imaginary". If you don't need the imaginary
numbers than pass an array of 0's.

Anyway, check out the link. I sure I won't be of any further use.
http://www.fullspectrum.com/deeth/programming/fft.html

mms
09-03-2003, 01:49 PM
I've used that FFT algorithm and it works great!

I was trying to do speed tests with this algorithm to see if it might be
faster.

Thanks for all your help! :D

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum