Measurement Studio for VB6

cancel
Showing results for 
Search instead for 
Did you mean: 

Help with conversion

I have an array Vav() that is about this many elements: 23,005,439.
I currently use this very slow routine to process the elements of the array and then find an average:
 
For b = 0 To UBound(Wav)
   If Wav(b) <> 0 Then
        N = 20 * (Log(Abs(Wav(b) / 2 ^   15))) / Log(10)
        Wav1(Cnt + 1) = N + 16.05015
        Cnt = Cnt + 1
   End If
Next
     
x = CWStat1.Mean(Wav1)
 
Is there a combination of CWSTAT and CWARRAY controls that I could use to process this much faster?
Thanks,
Bob Hiller
0 Kudos
Message 1 of 2
(6,188 Views)
Bob,

There are a few optimizations you can make on your code.

(1)  I'm not sure how sophisticated the VB6 compiler is, but modern compilers will usually recognize when something evaluates to a constant.  If you are performing an operation such as Log(10) over and over in a loop, this will always evaluate to the same value.  So there is no need to recalculate this value in every iteration of the loop.  Instead create some constants and pull those values out of the loop.

'Perform this calculation once before the loop
Const c1 as Double = 2 ^ 15

'With some simple algebra you can simply the 20 * (...) / Log(10) into 20 / Log(10) as one constant.  Unfortunately
'VB6 does not consider Log(10) as a constant expression but you can either assign 20 * Log(10) to a non constant
'variable or you can simply take Log(10) out of the equation since it evaluates to 1 anyway.
Const c2 as Double = 20  '* Log(10) = 1

(2)  There is a CWArray.AbsArray function that will return the Absolute Value of every item in an Array.  This may be slightly quicker than performing this inside the loop.

Wav = CWArray1.AbsArray(Wav)

(3)  There is also a CWArray.SearchArray function that will return an Array that contains only values of a certain range.  So, after getting the absolute value of the array, you could then use SearchArray based on bounds such as (0.0000001, 1000000 or Number Larger than any value in your Array).  This will remove all values of zero automatically.  Unfortunately, this function returns a two dimensional array, even if you perform it on a one dimensional array.  So it actually may be more efficient to use your current method of checking each value.

(4)  You are performing the Cnt + 1 operation twice.  To perform the operation only once, increment Cnt before you adjust Wav1(Cnt +1)

Cnt = Cnt + 1
Wav1(Cnt) = N + 16.05015

You should knock quite a few instructions out of your algorithm with these changes.

Let me know if you have further questions.

Thanks,

Tyler Tigue
0 Kudos
Message 2 of 2
(6,172 Views)