Define your terms! Distance is not possible because the rotary encoder just measures rotation and not a distance. You just get a distance if there is some kind of wheel attached to it, then you need the diameter of that wheel to calculate the distance.
What speed do you want? The average? The max?
from when it is started turning to when it is no longer turning anymore.
What is the time frame you're willing to accept that the encoder isn't rotated anymore? The rotary encoder just puts out pulses when there was a rotation of about 1/3°, so at about 1 RPM you don't get a pulse for over 60ms. Is that no rotation?
The best approach is to count the pulses for some amount of time. The size of that amount is depending largely on the answers to my questions above.
BTW: you should start your own thread.