Description
I take issue with the statement "When a sensor has to be read out frequently and one needs to map the value to another value e.g. to display or as control signal, the normal map() function is using more math operations than FastMap.map()"; this is misleading.
It may well be fewer instructions, but it takes over twice as long to perform them on an Arduino Mega; this is to be expected as FastMap
using floating point and map
uses integer fixed point.
I can understand when people need floating point, but to use it thinking that it's faster is a mistake.
StopWatch sw(StopWatch::MICROS);
volatile double d;
volatile byte b=0;
sw.start();
for(uint32_t i32 = 0; i32 < 1000; ++i32){++b;}
uint32_t t32_1K = sw.elapsed(); // = 760
sw.reset();
sw.start();
for(uint32_t i32 = 0; i32 < 1000; ++i32){ d = map(1234 + i32, 1000, 2000, 2000, 3000); }
uint32_t t32_map1000 = sw.elapsed(); // (*1000) = ~4912 with var
sw.reset();
FastMap fm;
fm.init(1000, 2000, 2000, 3000);
sw.start();
for(uint32_t i32 = 0; i32 < 1000; ++i32){ d = fm.map(1234 + i32); }
uint32_t t32_fmap1000 = sw.elapsed(); // (*1000) = ~11952
sw.reset();
Serial.print(F("start, loop 32 1000,elapsed=")); Serial.println(t32_1K, DEC);
Serial.print(F("start, loop 32 1000 map,elapsed=")); Serial.println(t32_map1000, DEC);
Serial.print(F("start, loop 32 1000 fmap,elapsed=")); Serial.println(t32_fmap1000, DEC);
Serial.print("b="); Serial.print(b,DEC); Serial.print(", d="); Serial.println(d, DEC);
results in
start, loop 32 1000,elapsed=756
start, loop 32 1000 map,elapsed=4912
start, loop 32 1000 fmap,elapsed=11952
b=165, d=3233.0000000000
This indicates that FastMap.map
is more than twice as slow as normal map
NOTE 1) My timings were done on an Arduino Mega 16MHz clone
NOTE 2) You MUST use variable d later on, otherwise the map
instruction gets optimised out (well, it does in my IDE)
NOTE 3) Code has been edited from longer run and may not compile cleanly, but you should be able to work it out.