I know this question is somewhat old but I also got interested in median filtering. If one is working with signals or images, then there will be a large overlap of data for the processing window. This can be taken advantage of.
I've posted some benchmark code here: 1D moving median filtering in C++
It's template based so it should work with most POD data types.
According to my results std::nth_element
has poor performance for a moving median, as it must sort the window of values each time.
However, using a pool of values that is kept sorted, one can perform the median with 3 operation.
- Remove oldest value out of the pool (calls std::lower_bound)
- Insert new value into pool (calls std::lower_bound)
- Store new value in history buffer
The median is now the middle value in the pool.
I hope someone finds this interesting and contributes their ideas!