You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The classes MultiStepper and AccelStepper frequently use double literals, such as 20.0 where float literals should be used (20.0f).
One particular example is AccelStepper's setSpeed() method - line 316 features the following:
_stepInterval = fabs(1000000.0 / speed);
As one can see, a double literal divided by the value of speed (a float).
When running the following code on an Arduino Uno R4 Wifi with the library unmodified, the execution of setSpeed() requires 833 CPU cycles as evidenced by cycles for setspeed(): 833 being printed on the serial. You may ignore the specifics, the potentially confusing-looking code simply measures the number of cycles before the start of and after the end of the execution of the setSpeed() invocation.
However, upon changing line 316 in the source code to be the following (using a float literal), the very same operation only requires 69 CPU cycles.
_stepInterval = fabs(1000000.0f / speed);
The following code (using an integer literal) does the job just as well with identical performance:
_stepInterval = fabs(1000000 / speed);
I would attribute this performance difference to the fact that the ARM Cortex M4 processor powering the Arduino Uno R4 has a Floating Precision Unit which only supports single precision, not double precision - with double precision operations requiring an immense amount of computational power. I have not tested if a similar performance difference is observed on Arduino Uno R3, the situation may be a bit different since it does not feature a Floating Precision Unit, but I would imagine that operations with floats would still be faster than operations with doubles.
If you need any more information, let me know.
I might work on a pull request addressing this issue at some point in the future.
The text was updated successfully, but these errors were encountered:
The classes
MultiStepper
andAccelStepper
frequently use double literals, such as20.0
where float literals should be used (20.0f
).One particular example is
AccelStepper
'ssetSpeed()
method - line 316 features the following:_stepInterval = fabs(1000000.0 / speed);
As one can see, a double literal divided by the value of speed (a float).
When running the following code on an Arduino Uno R4 Wifi with the library unmodified, the execution of
setSpeed()
requires 833 CPU cycles as evidenced bycycles for setspeed(): 833
being printed on the serial. You may ignore the specifics, the potentially confusing-looking code simply measures the number of cycles before the start of and after the end of the execution of the setSpeed() invocation.However, upon changing line 316 in the source code to be the following (using a float literal), the very same operation only requires 69 CPU cycles.
The following code (using an integer literal) does the job just as well with identical performance:
_stepInterval = fabs(1000000 / speed);
I would attribute this performance difference to the fact that the ARM Cortex M4 processor powering the Arduino Uno R4 has a Floating Precision Unit which only supports single precision, not double precision - with double precision operations requiring an immense amount of computational power. I have not tested if a similar performance difference is observed on Arduino Uno R3, the situation may be a bit different since it does not feature a Floating Precision Unit, but I would imagine that operations with floats would still be faster than operations with doubles.
If you need any more information, let me know.
I might work on a pull request addressing this issue at some point in the future.
The text was updated successfully, but these errors were encountered: