# why MeanFilter is nearly 100 times slower than MovingAverage for 1D array?

I used to average over my data with MeanFilter before I learn about MovingAverage and thought that these two functions are similar except that MeanFilter is more powerful for its ability to deal with 2D data. However,even with 1D data I prefer MeanFilter because it will not shorten the list which saves my effort for taking care of the length of data list until I found that MeanFilter is surprisingly slow:

for example define the data:

dat = RandomReal[{0, 1}, 2000000];


caculate time consuming:

timeofmoav =
AbsoluteTiming[MovingAverage[dat[[1 ;; #]], 5];] & /@ (2^Range[10, 20])


and

timeofmefi =
AbsoluteTiming[MeanFilter[ dat[[1 ;; #]], 2][[3 ;; -3]];] & /@ (2^Range[10,20])


plot by:

ListLogLogPlot[
Transpose[{2^Range[10, 20], #}] & /@ {timeofmoav[[All, 1]],
timeofmefi[[All, 1]]}, Joined -> True, PlotStyle -> Thick,
PlotRange -> All, Frame -> True, ImageSize -> 500,
FrameTicksStyle -> 15, GridLines -> Automatic,
GridLinesStyle -> Directive[Orange, Dashed],
FrameLabel -> {"dat", "time consumed"}, LabelStyle -> 20,
PlotLegends ->
Placed[LineLegend[{"MovingAverage", "MeanFilter"},
LegendFunction -> "Panel"], {0.2, 0.8}]]


which looks like:

how should MeanFilter be so slow?

• I suppose, it is due to the broader scope of MeanFilter (image processing and such). Apr 17, 2015 at 15:04

It is because they use different algorithms.

MeanFilter is one of a family of filters which use DeveloperPartitionMap internally. This means that Mathematica is separately computing the mean of each run of 5 elements in your data set.

MovingAverage is based on ListCorrelate which uses a fast FFT method. The documentation states:

MovingAverage[list,wts] is equivalent to ListCorrelate[wts/Total[wts],list]. »

and in fact this is essentially the code used internally.

MeanFilter uses different algorithms (as noted by Simon), and is optimized for images (ability to use on "plain" data is a nice feature here, more so with some of the other image processing functions).

This can be seen by doing :

ImageData[MeanFilter[Image@{dat}, {0, 2}]][[1, 3 ;; -3]]
`

Resulting in roughly comparable timings and the same result as your example.