View previous topic :: View next topic |
Author |
Message |
FMOpt2000
Joined: 10 Jan 2008 Posts: 17
|
Float performance |
Posted: Wed Jan 25, 2012 9:42 am |
|
|
Hi,
I've a firmware on which I need to normalize some result.
The firmware has a polling structure and the float division use a lot of time.
I don't need a particular precision.
When the firmware starts, reads from EEPROM 2 values and I calculate
the "normalizzazione" variables (L=left,R=right).
During the polling, I do the float computation.
Is there a way to reduce the time computation in some way (reducing the precision)?
Here, part (incomplete) of firmware:
Code: |
main()
{
limit_foot_h_left=read_EEPROM(0x00);
limit_foot_h_right=read_EEPROM(0x01);
normalizzazione_L=((127-limit_foot_h_left)/100.0);
normalizzazione_R=((limit_foot_h_right-127)/100.0);
while (1)
{
pot_lev_2=127-(127-pot_lev_2)/normalizzazione_L;
}
}
|
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Wed Jan 25, 2012 10:40 am |
|
|
Quickest solution is to NOT use floats ! Really, just use simple int8 bit math as the data appears to be 8 bits anyway ?
Also consider /128 as opposed to /100.0 . That will shave a LOT of time off the 'math', though you may need to adjust the result.
Since we don't know the range or precision or accuracy required it's hard to guess which is better overall since the application is unknown to us. |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Wed Jan 25, 2012 4:33 pm |
|
|
Also note that multiplication is faster than division. If you can precalculate X=1/normalizzazione_L and then multiply by X when needed it will be faster.
But if there is ANY way to use long ints instead of floats it will be much faster.
consider:
pot_lev_2=127-(127-pot_lev_2) * Xnormint / 256;
where Xnormint is a precalculated 16 bit int. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
FMOpt2000
Joined: 10 Jan 2008 Posts: 17
|
|
Posted: Thu Jan 26, 2012 8:39 am |
|
|
Good,
I've improved a lot the polling rate, but in some cases
the RS232 is blocked by an over-rate.
Is there a way to reset/flush the buffer or port? |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Thu Jan 26, 2012 9:18 am |
|
|
you should have 'errors' added to the use rs232(....) options.... |
|
|
|