Float vs Int (was: [sdiy] programmable synth module)
David Brown
davebr at earthlink.net
Tue Feb 14 06:49:37 CET 2006
At 01:07 AM 2/12/2006, Andre Majorel wrote:
> > > Remember convert all numbers to floating point for speed improvement.
> >
> > Totally counter-intuitive to us old and semi-old timers!
This bothered me since I have been optimizing my code to use integers
(guess I'm an old timer). I decided to run a test on my PSIM. I
used a line of my code that displays DAC voltages on the LCD as the
test case. I need to divide by 102.3 and 10.23 so I had coded this
using integers by first multiplying and then dividing (as *10/1023
and *100/1023).
I measured the execution speed with my scope and the integer
calculation took 115.6 uS.
i1 var word
i2 var word
let i1=8
let i2=((511-((i1*1023)/10))*100)/1023 ;executes in 115.6 uS
Doing this same calculation using floating point variables took 50% longer
f1 var float
f2 var float
let f1=8.0
let f2=((511.0-((f1*1023.0)/10.0))*100.0)/1023.0 ;executes in 176 uS
However, with floating point I don't need to do the extra multiplications.
Simplifying the statement to dividing by 102.3 and 10.23 resulted in
code that was only 12% slower than using integer variables
f1 var float
f2 var float
let f1=8.0
let f2=(511.0-(f1/102.3))/10.23 ;executes in 130.0 uS
I guess I'll start using floating point for some of my calculations
since there really isn't any penalty other than ram storage.
Dave
More information about the Synth-diy
mailing list