[squeak-dev] Re: [Vm-dev] MadgwickAHRS plugin and support code

Herbert König herbertkoenig at gmx.net
Sun Sep 13 11:34:32 UTC 2015


Hi,

Am 13.09.2015 um 06:31 schrieb David T. Lewis:
> Well it's basically the only convenient way to deal with float (as 
> opposed to double) floating point values. In Squeak, a Float is a C 
> double, and AFIK the only thing that directly represents single 
> precision floats is FloatArray. You would be justified in being scared 
> of them, especially if any of this sounds like it makes sense. But it 
> does work and it might be more efficient than arrays of Float (C 
> doubles). Or maybe not, I don't have any way to measure it. But I 
> tried to set things up so that the FloatArray things can be reused to 
> avoid allocations and type conversions, so maybe it will help. 

FloatArray offers a lot of speed up as long as the operations provided 
by Float array are sufficient. Getting Squeak Floats into and out of 
Float Array is expensive.
This:
Time millisecondsToRun:
     [|array1 array2|
     array1 := (1.0 to: 10000.0 by: 0.1) asArray.
     array2 := (10000.0 to: 1.0 by: -0.1) asArray.
     1000 timesRepeat: [|result|
         result := array1  * array2]]
22 seconds
is not much slower than using FloatArray
Time millisecondsToRun:
     [|array1 array2|
     array1 := (1.0 to: 10000.0 by: 0.1) asArray.
     array2 := (10000.0 to: 1.0 by: -0.1) asArray.
     1000 timesRepeat: [|result|
         result := (array1 asFloatArray * array2 asFloatArray)]]
19 seconds
If you change the last line to use more operations supported by 
FloatArray like:
         result := (array1 asFloatArray * array2 asFloatArray + 7.0 * 
3.0) sum
with FloatArrays you get the same speed, without the conversion to 
FloatArray it takes three times as long.

If you need to use FloatArray>>at: and #at:put very often FloatArrays 
may get slower than Arrays of Float.
So benchmark and benchmark again.

And if your values get small, FloatArray may round them to zero which is 
bad for division.

I used FloatArray a lot for  Audio processing and neural networks but 
they need to be handled with care.
I ended up using both depending on the algorithm.

Cheers,

Herbert




More information about the Squeak-dev mailing list