PaulStoffregen / Time

Time library for Arduino
http://playground.arduino.cc/code/time
1.24k stars 664 forks source link

wasteful loop in now() #157

Open pidloop opened 3 years ago

pidloop commented 3 years ago

In Time.cpp, the loop at the beginning of now() is wasteful. It could be replaced with:

uint32_t n_secs = (millis() - prevMillis)/1000U;
sysTime += n_secs;
prevMillis += n_secs * 1000U;

Also, the nearby comment about subtracting unsigned values is wrong. Assuming the expression A-B, it's not the absolute value that is the same, it's that the distance from B towards A correctly accounts for overflow when A < B.

Cheers!