Please post comments and improvements in this thread,
You asked for it.
First... Thank you for the contribution.
void RunningMedian::add(long value)
{
...
_idx %= _size;
...modulus is expensive. Use if instead...
void RunningMedian::add(long value)
{
...
if ( _idx >= _size )
_idx = 0;
protected:
int _size;
int _cnt;
int _idx;
It's very unlikely the size, count, and index need to be a full integer. An 8-bit data-type is more appropriate. You may have to continue using a signed data-type.
Given the fact that _size is a fixed size, it should be turned into a static const.
I don't think it's necessary to perform a complete sort. I believe a heap would get the result you want.
I believe 5 is a very good choice for _size. Given the fact that _size is fixed at 5, you may want to "unroll" the loops. I suspect the total code size will be reduced and the execution time will obviously improve.